-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (50 loc) · 1.81 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
const exec = require('./exec');
const listProcessesOnPort = module.exports.listProcessesOnPort = async port => {
const portNumber = parseInt(port, 10);
if (Number.isNaN(portNumber)) {
console.error("Must provide number for port.");
return;
}
try {
const result = (await exec(`lsof -i :${portNumber}`)).output.split('\n');
const headers = result.shift().split(' ').filter(item => !!item.trim() && item.trim() !== "").map(item => item.toLowerCase());
return result.filter(item => !!item.trim() && item.trim() !== "").reduce((accumulator, currentValue) => {
accumulator.push(currentValue.split(' ').filter(item => !!item.trim() && item.trim() !== "").reduce((accumulator, currentValue, index) => {
if (index > headers.length - 1) {
accumulator[headers[headers.length - 1]] = (!!accumulator[headers[headers.length - 1]].trim() && accumulator[headers[headers.length - 1]].trim() !== "") ? `${accumulator[headers[headers.length - 1]]} ${currentValue}` : currentValue;
} else {
accumulator[headers[index]] = currentValue;
}
return accumulator;
}, {}));
return accumulator;
}, []);
} catch (e) {
console.error(e);
}
};
const killProcess = module.exports.killProcess = async pid => {
const pidNumber = parseInt(pid, 10);
if (Number.isNaN(pidNumber)) {
console.error("Must provide number for process identifier.");
return false;
}
try {
await exec(`kill ${pidNumber}`);
return true;
} catch (e) {
return false;
}
};
const killAllProcessesOnPort = module.exports.killAllProcessesOnPort = async port => {
try {
const processesOnPort = await listProcessesOnPort(port);
const killProcessResult = processesOnPort.map(theProcess => {
const success = killProcess(theProcess.pid);
return {pid: theProcess.pid, success};
});
return killProcessResult;
} catch (e) {
console.log(e);
}
};