-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
68 lines (59 loc) · 1.53 KB
/
index.mjs
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
await switch2cmd();
let [option] = process.argv.slice(2);
option = (option ?? "").trim();
const pidReg = /^-p:/i;
const portReg = /^-i:/i;
if (!option || (!pidReg.test(option) && !portReg.test(option))) {
console.log(`
lsof -i:port 查询占用port的进程
lsof -p:pid 查询进程ID为pid的进程
`);
process.exit(1);
}
let pidList = [];
if (portReg.test(option)) {
const inputPort = option.trim().replace(portReg, "");
const pidResult =
await $.toString2`netstat -aon | findstr :${inputPort}`.catch((e) => {
return "";
});
pidList = Array.from(
new Set(
pidResult.split(/[\r\n]+/).map((line) => {
return line.replace(/.*\s+/, "");
})
)
)
.filter((port) => {
return port !== "0" && port !== "";
})
.sort()
.reverse();
} else if (pidReg.test(option)) {
pidList.push(option.replace(pidReg, ""));
}
const taskStr = await $.toString2`tasklist`;
const tasks = taskStr.split(/[\r\n]+/);
const taskMap = tasks.reduce((r, line) => {
const temp = line.trim().split(/\s{4,}/);
const pid = (temp[1] ?? "").trim().replace(/[^\d]+/, "");
r[pid] = [temp[0], temp[4] ?? ""];
return r;
}, {});
const result = pidList
.map((pid) => {
return [pid, ...(taskMap[pid] ?? [])];
})
.map((list) => {
return (
list
.map((str, index) => {
return str.padStart(index === 0 ? 6 : index === 1 ? 20 : 10, " ");
})
.join(" ") +
" " +
`TASKKILL /PID ${list[0]} /T`
);
})
.join("\n");
console.log(result);