-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
130 lines (103 loc) · 2.86 KB
/
index.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
const arrify = require('arrify');
const taskkill = require('taskkill');
const execa = require('execa');
const AggregateError = require('aggregate-error');
const pidFromPort = require('pid-from-port');
const processExists = require('process-exists');
const psList = require('ps-list');
const missingBinaryError = async (command, arguments_) => {
try {
return await execa(command, arguments_);
} catch (error) {
if (error.code === 'ENOENT') {
const newError = new Error(`\`${command}\` doesn't seem to be installed and is required by fkill`);
newError.sourceError = error;
throw newError;
}
throw error;
}
};
const windowsKill = (input, options) => {
return taskkill(input, {
force: options.force,
tree: typeof options.tree === 'undefined' ? true : options.tree
});
};
const macosKill = (input, options) => {
const killByName = typeof input === 'string';
const command = killByName ? 'pkill' : 'kill';
const arguments_ = [input];
if (options.force) {
arguments_.unshift('-9');
}
if (killByName && options.ignoreCase) {
arguments_.unshift('-i');
}
return missingBinaryError(command, arguments_);
};
const defaultKill = (input, options) => {
const killByName = typeof input === 'string';
const command = killByName ? 'killall' : 'kill';
const arguments_ = [input];
if (options.force) {
arguments_.unshift('-9');
}
if (killByName && options.ignoreCase) {
arguments_.unshift('-I');
}
return missingBinaryError(command, arguments_);
};
const kill = (() => {
if (process.platform === 'darwin') {
return macosKill;
}
if (process.platform === 'win32') {
return windowsKill;
}
return defaultKill;
})();
const parseInput = async input => {
if (typeof input === 'string' && input[0] === ':') {
return pidFromPort(parseInt(input.slice(1), 10));
}
return input;
};
const fkill = async (inputs, options = {}) => {
inputs = arrify(inputs);
const exists = await processExists.all(inputs);
const errors = [];
const handleKill = async input => {
try {
input = await parseInput(input);
if (input === process.pid) {
return;
}
if (input === 'node') {
const processes = await psList();
await Promise.all(processes.map(async ps => {
if (ps.name === 'node' && ps.pid !== process.pid) {
await kill(ps.pid, options);
}
}));
return;
}
await kill(input, options);
} catch (error) {
if (!exists.get(input)) {
errors.push(`Killing process ${input} failed: Process doesn't exist`);
return;
}
errors.push(`Killing process ${input} failed: ${error.message.replace(/.*\n/, '').replace(/kill: \d+: /, '').trim()}`);
}
};
await Promise.all(
inputs.map(input => handleKill(input))
);
if (errors.length > 0 && !options.silent) {
throw new AggregateError(errors);
}
};
module.exports = fkill;
// TODO: remove this in the next major version
module.exports.default = fkill;