-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill.ts
105 lines (105 loc) · 3.32 KB
/
kill.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
import {
getProcessInfo,
getProcessInfoSync,
type ProcessGetInfoOptions,
type ProcessInfo
} from "./get.ts";
export type {
ProcessGetInfoOptions
};
/**
* Force end of the process, asynchronously.
* @param {number | string} input ID or name of the process.
* @param {Omit<ProcessGetInfoOptions, "depth">} [options={}] Options.
* @returns {Promise<void>}
*/
export async function killProcess(input: number | string, options?: Omit<ProcessGetInfoOptions, "depth">): Promise<void>;
/**
* Force end of the processes, asynchronously.
* @param {readonly (number | string)[]} inputs ID or name of the processes.
* @param {Omit<ProcessGetInfoOptions, "depth">} [options={}] Options.
* @returns {Promise<void>}
*/
export async function killProcess(inputs: readonly (number | string)[], options?: Omit<ProcessGetInfoOptions, "depth">): Promise<void>;
export async function killProcess(param0: number | string | readonly (number | string)[], options?: Omit<ProcessGetInfoOptions, "depth">): Promise<void> {
const fails: Error[] = [];
for (const input of (Array.isArray(param0) ? param0 : [param0])) {
if (typeof input === "number") {
try {
Deno.kill(input);
} catch (error) {
fails.push(error as Error);
}
} else if (typeof input === "string") {
let processes: ProcessInfo[] = [];
try {
processes = (await getProcessInfo(input, {
...options,
depth: 0
})).results;
} catch (error) {
fails.push(error as Error);
}
for (const process of processes) {
try {
Deno.kill(process.id);
} catch (error) {
fails.push(error as Error);
}
}
} else {
fails.push(new Error(`Invalid process input \`${input}\`!`));
}
}
if (fails.length > 0) {
throw new AggregateError(fails, `Unable to kill some of the processes!`);
}
}
/**
* Force end of the process, synchronously.
* @param {number | string} input ID or name of the process.
* @param {Omit<ProcessGetInfoOptions, "depth">} [options={}] Options.
* @returns {void}
*/
export function killProcessSync(input: number | string, options?: Omit<ProcessGetInfoOptions, "depth">): void;
/**
* Force end of the processes, synchronously.
* @param {readonly (number | string)[]} inputs ID or name of the processes.
* @param {Omit<ProcessGetInfoOptions, "depth">} [options={}] Options.
* @returns {void}
*/
export function killProcessSync(inputs: readonly (number | string)[], options?: Omit<ProcessGetInfoOptions, "depth">): void;
export function killProcessSync(param0: number | string | readonly (number | string)[], options?: Omit<ProcessGetInfoOptions, "depth">): void {
const fails: Error[] = [];
for (const input of (Array.isArray(param0) ? param0 : [param0])) {
if (typeof input === "number") {
try {
Deno.kill(input);
} catch (error) {
fails.push(error as Error);
}
} else if (typeof input === "string") {
let processes: ProcessInfo[] = [];
try {
processes = getProcessInfoSync(input, {
...options,
depth: 0
}).results;
} catch (error) {
fails.push(error as Error);
}
for (const process of processes) {
try {
Deno.kill(process.id);
} catch (error) {
fails.push(error as Error);
}
}
} else {
fails.push(new Error(`Invalid process input \`${input}\`!`));
}
}
if (fails.length > 0) {
throw new AggregateError(fails, `Unable to kill some of the processes!`);
}
}