-
Notifications
You must be signed in to change notification settings - Fork 2
/
stop-processes.mjs
77 lines (70 loc) · 2.65 KB
/
stop-processes.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* eslint-disable no-console */
import { exec } from 'child_process';
import fkill from 'fkill';
import { indexOf, lastIndexOf, includes } from 'platform-bible-utils';
// All processes with any of these terms in the command line will be killed
const searchTerms = ['electronmon', 'esbuild', 'nodemon', 'vite', 'webpack', 'extension-host'];
// Don't display errors because we try to kill processes that already died because of a previous kill
const fkillOptions = { silent: true, forceAfterTimeout: 1000 };
function killProcessesWithSearchTerm() {
// Different platforms have different ways to get running process info
let listCommand;
if (process.platform === 'win32') {
listCommand = 'WMIC path win32_process get Commandline,Processid /format:csv';
} else if (process.platform === 'darwin' || process.platform === 'linux') {
listCommand = 'ps -A -o pid,command';
} else {
console.error('Unsupported platform:', process.platform);
return;
}
// Get the list of running processes
exec(listCommand, async (error, stdout) => {
if (error) {
console.error(`Error executing command: ${error}`);
return;
}
// Process the output to find processes with a search term
let processes;
if (process.platform === 'win32') {
processes = stdout
.split('\n')
.slice(1)
.map((line) => {
const firstIndex = indexOf(line, ',');
const lastIndex = lastIndexOf(line, ',');
const pid = line.substring(lastIndex + 1);
const command = line.substring(firstIndex + 1, lastIndex);
return { pid, command };
});
} else if (process.platform === 'darwin' || process.platform === 'linux') {
processes = stdout
.split('\n')
.slice(1)
.map((line) => {
const trimmedLine = line.trim();
const index = indexOf(trimmedLine, ' ');
const pid = trimmedLine.substring(0, index);
const command = trimmedLine.substring(index + 1);
return { pid, command };
});
} else {
console.log(`Unexpected platform: ${process.platform}`);
process.exit(-1);
}
// Kill the processes with a search term in process name or arguments
await Promise.all(
processes.map(async ({ pid, command }) => {
if (command && pid && searchTerms.some((term) => includes(command, term))) {
console.log(`Killing ${command}`);
return fkill(Number(pid), fkillOptions);
}
return Promise.resolve();
}),
);
});
}
// DO IT
(async () => {
killProcessesWithSearchTerm();
await Promise.all([fkill('electron', fkillOptions), fkill('dotnet', fkillOptions)]);
})();