forked from gilamran/tsc-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkiller.js
37 lines (33 loc) · 964 Bytes
/
killer.js
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
const psTree = require('ps-tree');
const spawn = require('cross-spawn');
const {exec} = require('child_process');
let KILL_SIGNAL = 'SIGUSR2';
let hasPS = true;
const isWindows = process.platform === 'win32';
// discover if the OS has `ps`, and therefore can use psTree
exec('ps', function (error) {
if (error) {
hasPS = false;
}
});
module.exports = function kill(child) {
return new Promise((resolve, reject) => {
if (isWindows) {
exec('taskkill /pid ' + child.pid + ' /T /F');
resolve();
} else {
if (hasPS) {
psTree(child.pid, function (err, kids) {
spawn('kill', ['-s', KILL_SIGNAL, child.pid].concat(kids.map(function (p) {
return p.PID;
}))).on('close', resolve);
});
} else {
exec('kill -s ' + KILL_SIGNAL + ' ' + child.pid, function () {
// ignore if the process has been killed already
resolve();
});
}
}
});
};