Open History to see the last commit
1import * as NodeChildProcess from "node:child_process";
2import * as NodePath from "node:path";
3
4export function resolveTaskkillExecutable(env = process.env) {
5 const windowsRoot = env.SystemRoot?.trim() || env.WINDIR?.trim();
6 return windowsRoot
7 ? NodePath.win32.join(windowsRoot, "System32", "taskkill.exe")
8 : "taskkill.exe";
9}
10
11export function terminateWindowsProcessTree(
12 pid,
13 { force = false, env = process.env, spawnSync = NodeChildProcess.spawnSync } = {},
14) {
15 if (!Number.isInteger(pid) || pid <= 0) {
16 return { attempted: false, ok: false, status: null };
17 }
18
19 const args = ["/PID", String(pid), "/T", ...(force ? ["/F"] : [])];
20 const result = spawnSync(resolveTaskkillExecutable(env), args, {
21 stdio: "ignore",
22 windowsHide: true,
23 });
24
25 return {
26 attempted: true,
27 ok: result.status === 0,
28 status: result.status,
29 };
30}