forked from sindresorhus/fkill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (36 loc) · 1015 Bytes
/
index.js
File metadata and controls
44 lines (36 loc) · 1015 Bytes
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
'use strict';
const arrify = require('arrify');
const taskkill = require('taskkill');
const execa = require('execa');
const AggregateError = require('aggregate-error');
function win(input, opts) {
return taskkill(input, {
force: opts.force,
// don't kill ourselves
filter: `PID ne ${process.pid}`
});
}
function def(input, opts) {
const cmd = typeof input === 'string' ? 'killall' : 'kill';
const args = [input];
if (opts.force) {
args.unshift('-9');
}
return execa(cmd, args);
}
module.exports = (input, opts) => {
opts = opts || {};
const fn = process.platform === 'win32' ? win : def;
const errors = [];
// don't kill ourselves
input = arrify(input).filter(x => x !== process.pid);
return Promise.all(input.map(input => {
return fn(input, opts).catch(err => {
errors.push(`Killing process ${input} failed: ${err.message.replace(/.*\n/, '').replace(/kill: \d+: /, '').trim()}`);
});
})).then(() => {
if (errors.length > 0) {
throw new AggregateError(errors);
}
});
};