blob: 76b32cc54fc6e06fcca15a5e5a3e3576d3496101 (
plain)
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
|
// @see vite.config.ts comments for an explanation of why this file exists.
//
// These functions are copied from vite-plugin-electron, MIT licensed.
// https://github.com/electron-vite/vite-plugin-electron/blob/c58fbf9c674f08b020455f01c97b99d3d7b562a9/src/utils.ts
import cp from 'node:child_process';
export interface PidTree {
pid: number;
ppid: number;
children?: PidTree[];
}
export function treeKillSync(pid: number) {
if (process.platform === 'win32') {
cp.execSync(`taskkill /pid ${pid} /T /F`);
} else {
killTree(pidTree({ pid, ppid: pid }));
}
}
function pidTree(tree: PidTree) {
const command =
process.platform === 'darwin'
? `pgrep -P ${tree.pid}` // Mac
: `ps -o pid --no-headers --ppid ${tree.ppid}`; // Linux
try {
const childs = cp
.execSync(command, { encoding: 'utf8' })
.match(/\d+/g)
?.map((id) => +id);
if (childs) {
tree.children = childs.map((cid) => pidTree({ pid: cid, ppid: tree.pid }));
}
} catch {
// Do nothing
}
return tree;
}
function killTree(tree: PidTree) {
if (tree.children) {
for (const child of tree.children) {
killTree(child);
}
}
try {
process.kill(tree.pid); // #214
} catch {
/* empty */
}
}
|