-
Notifications
You must be signed in to change notification settings - Fork 899
Expand file tree
/
Copy pathprocess.mjs
More file actions
155 lines (138 loc) · 4.51 KB
/
process.mjs
File metadata and controls
155 lines (138 loc) · 4.51 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { spawnSync } from "node:child_process";
import process from "node:process";
export function runCommand(command, args = [], options = {}) {
const result = spawnSync(command, args, {
cwd: options.cwd,
env: options.env,
encoding: "utf8",
input: options.input,
maxBuffer: options.maxBuffer,
stdio: options.stdio ?? "pipe",
shell: process.platform === "win32" ? (process.env.SHELL || true) : false,
windowsHide: true
});
return {
command,
args,
status: result.status ?? 0,
signal: result.signal ?? null,
stdout: result.stdout ?? "",
stderr: result.stderr ?? "",
error: result.error ?? null
};
}
export function runCommandChecked(command, args = [], options = {}) {
const result = runCommand(command, args, options);
if (result.error) {
throw result.error;
}
if (result.status !== 0) {
throw new Error(formatCommandFailure(result));
}
return result;
}
export function binaryAvailable(command, versionArgs = ["--version"], options = {}) {
const result = runCommand(command, versionArgs, options);
if (result.error && /** @type {NodeJS.ErrnoException} */ (result.error).code === "ENOENT") {
return { available: false, detail: "not found" };
}
if (result.error) {
return { available: false, detail: result.error.message };
}
if (result.status !== 0) {
const detail = result.stderr.trim() || result.stdout.trim() || `exit ${result.status}`;
return { available: false, detail };
}
return { available: true, detail: result.stdout.trim() || result.stderr.trim() || "ok" };
}
function looksLikeMissingProcessMessage(text) {
return /not found|no running instance|cannot find|does not exist|no such process/i.test(text);
}
/**
* Checks whether a process with the given PID is still running.
* Uses signal 0 which does not affect the process - it only checks existence.
*
* @param {number | null | undefined} pid
* @returns {boolean}
*/
export function isProcessAlive(pid) {
if (pid == null || !Number.isFinite(pid) || pid <= 0) {
return false;
}
try {
process.kill(pid, 0);
return true;
} catch (error) {
// ESRCH = no such process (dead). EPERM = exists but no signal permission.
return error?.code === "EPERM";
}
}
export function terminateProcessTree(pid, options = {}) {
if (!Number.isFinite(pid)) {
return { attempted: false, delivered: false, method: null };
}
const platform = options.platform ?? process.platform;
const runCommandImpl = options.runCommandImpl ?? runCommand;
const killImpl = options.killImpl ?? process.kill.bind(process);
if (platform === "win32") {
const result = runCommandImpl("taskkill", ["/PID", String(pid), "/T", "/F"], {
cwd: options.cwd,
env: options.env
});
if (!result.error && result.status === 0) {
return { attempted: true, delivered: true, method: "taskkill", result };
}
const combinedOutput = `${result.stderr}\n${result.stdout}`.trim();
if (!result.error && looksLikeMissingProcessMessage(combinedOutput)) {
return { attempted: true, delivered: false, method: "taskkill", result };
}
if (result.error?.code === "ENOENT") {
try {
killImpl(pid);
return { attempted: true, delivered: true, method: "kill" };
} catch (error) {
if (error?.code === "ESRCH") {
return { attempted: true, delivered: false, method: "kill" };
}
throw error;
}
}
if (result.error) {
throw result.error;
}
throw new Error(formatCommandFailure(result));
}
try {
killImpl(-pid, "SIGTERM");
return { attempted: true, delivered: true, method: "process-group" };
} catch (error) {
if (error?.code !== "ESRCH") {
try {
killImpl(pid, "SIGTERM");
return { attempted: true, delivered: true, method: "process" };
} catch (innerError) {
if (innerError?.code === "ESRCH") {
return { attempted: true, delivered: false, method: "process" };
}
throw innerError;
}
}
return { attempted: true, delivered: false, method: "process-group" };
}
}
export function formatCommandFailure(result) {
const parts = [`${result.command} ${result.args.join(" ")}`.trim()];
if (result.signal) {
parts.push(`signal=${result.signal}`);
} else {
parts.push(`exit=${result.status}`);
}
const stderr = (result.stderr || "").trim();
const stdout = (result.stdout || "").trim();
if (stderr) {
parts.push(stderr);
} else if (stdout) {
parts.push(stdout);
}
return parts.join(": ");
}