-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.ts
120 lines (112 loc) · 3.62 KB
/
cli.ts
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
import { Checkbox, Command } from "./deps.ts";
import { dkill } from "./mod.ts";
import { procList } from "./src/procList.ts";
import { upgrader } from "./src/upgrader.ts";
import { assertMinVersion } from "./src/utils/versions.ts";
import vJson from "./deno.json" with { type: "json" };
// check minimum version of deno
const minVRequired = "1.31.1"; // uses deno.Command
if (!assertMinVersion(Deno.version.deno, minVRequired)) {
console.error(
`Please upgrade deno. Minimum version required is: ${minVRequired}`,
);
Deno.exit(1);
}
export const run = async () => {
await new Command()
.name("dkill")
.version(vJson.version)
.description(
`Kill any processes by
- port: Prefix port number by a colon. ex: 'dkill :3000'
- pid: A valid integer. ex: 'dkill 12654'
- process name: A string ex: 'dkill Code.exe'
You can specify multiple targets at once: 'dkill node.exe :5000 :3000 164'`,
)
.arguments("<...targets>")
.option("-i, --interactive", "Interactive mode (Not available on MacOS)", {
standalone: true,
})
.option("-v, --verbose", "Increase verbosity")
.option(
"-d, --dryrun",
"Dry run, List the pids that would have been killed. Does not kill anything",
)
.option(
"-u, --upgrade",
"Print out the command to upgrade if a new version is found. This will not process any other command",
{
standalone: true,
},
)
.action(
async (opts, ...targets) => {
if (opts.upgrade) {
// upgrading version.
await upgrader({
packageName: "dkill",
currentVersion: vJson.version,
});
return;
}
const ports: number[] = [];
const pids: number[] = [];
const procs: string[] = [];
if (opts.interactive) {
if (Deno.build.os === "darwin") {
console.error("Not implemented on macos");
Deno.exit(1);
}
// list processes
const pList = await procList();
const pickedProcesses: string[] = await Checkbox.prompt<string>({
message: "Pick processes to kill",
options: pList.map((item) => ({
name: `${item.pid} | ${item.proc} | ${item.cmd}`,
value: `${item.pid}`,
})),
search: true,
});
pickedProcesses.forEach((p) => pids.push(+p));
} else {
targets.forEach((target) => {
// Check if port
if (target.startsWith(":")) {
const port = +target.slice(1);
if (!Number.isInteger(port)) {
console.log(`Invalid port number "port"`);
return;
}
ports.push(port);
} else if (Number.isInteger(+target)) {
// check if pid
pids.push(+target);
} else {
// must be a string
procs.push(target);
}
});
}
const killed = await dkill(
{ ports, pids, procs },
{
verbose: opts.verbose,
dryrun: opts.dryrun,
includeCmds: true,
},
);
if (killed && killed.length) {
// TODO improve table output
// console.table(killed.map(pidItem => ({ ...pidItem, port: `:${pidItem.port}`, killed: pidItem.killed ? 'yes' : 'x'})));
console.table(killed);
opts?.dryrun && console.log("Nothing has been killed");
} else {
console.log("No process found");
}
},
)
.parse(Deno.args);
};
if (import.meta.main) {
await run();
}