























@@ -1,4 +1,5 @@
11import { spawn } from "node:child_process";
2+import { buildCmdExeCommandLine } from "../windows-cmd-helpers.mjs";
2334const FORWARDED_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
45const FORCE_KILL_DELAY_MS = 5_000;
@@ -48,6 +49,9 @@ function terminateManagedChild(child, signal = "SIGTERM") {
4849 * env?: NodeJS.ProcessEnv;
4950 * stdio?: import("node:child_process").StdioOptions;
5051 * shell?: boolean;
52+ * windowsVerbatimArguments?: boolean;
53+ * platform?: NodeJS.Platform;
54+ * comSpec?: string;
5155 * onReady?: (child: import("node:child_process").ChildProcess) => void;
5256 * }} options
5357 * @returns {Promise<number>}
@@ -59,15 +63,23 @@ export async function runManagedCommand({
5963 env,
6064 stdio = "inherit",
6165 shell = process.platform === "win32",
66+ windowsVerbatimArguments,
67+ platform = process.platform,
68+ comSpec,
6269 onReady,
6370}) {
64-const child = spawn(bin, args, {
71+const spawnSpec = createManagedCommandSpawnSpec({
72+ bin,
73+ args,
6574 cwd,
6675 env,
6776 stdio,
6877 shell,
69-detached: process.platform !== "win32",
78+ windowsVerbatimArguments,
79+ platform,
80+ comSpec,
7081});
82+const child = spawn(spawnSpec.command, spawnSpec.args, spawnSpec.options);
71837284let receivedSignal = null;
7385let forceKillTimer = null;
@@ -102,6 +114,91 @@ export async function runManagedCommand({
102114}
103115}
104116117+/**
118+ * @param {{
119+ * bin: string;
120+ * args?: string[];
121+ * cwd?: string;
122+ * env?: NodeJS.ProcessEnv;
123+ * stdio?: import("node:child_process").StdioOptions;
124+ * shell?: boolean;
125+ * windowsVerbatimArguments?: boolean;
126+ * platform?: NodeJS.Platform;
127+ * comSpec?: string;
128+ * }} options
129+ */
130+export function createManagedCommandSpawnSpec({
131+ bin,
132+ args = [],
133+ cwd,
134+ env,
135+ stdio = "inherit",
136+ shell = process.platform === "win32",
137+ windowsVerbatimArguments,
138+ platform = process.platform,
139+ comSpec,
140+}) {
141+const invocation = createManagedCommandInvocation({
142+ bin,
143+ args,
144+ env,
145+ shell,
146+ windowsVerbatimArguments,
147+ platform,
148+ comSpec,
149+});
150+151+return {
152+args: invocation.args,
153+command: invocation.command,
154+options: {
155+ cwd,
156+ env,
157+ stdio,
158+shell: invocation.shell,
159+detached: platform !== "win32",
160+windowsVerbatimArguments: invocation.windowsVerbatimArguments,
161+},
162+};
163+}
164+165+/**
166+ * @param {{
167+ * bin: string;
168+ * args?: string[];
169+ * env?: NodeJS.ProcessEnv;
170+ * shell?: boolean;
171+ * windowsVerbatimArguments?: boolean;
172+ * platform?: NodeJS.Platform;
173+ * comSpec?: string;
174+ * }} options
175+ */
176+export function createManagedCommandInvocation({
177+ bin,
178+ args = [],
179+ env,
180+ shell = process.platform === "win32",
181+ windowsVerbatimArguments,
182+ platform = process.platform,
183+ comSpec,
184+}) {
185+if (platform === "win32" && shell && args.length > 0) {
186+return {
187+args: ["/d", "/s", "/c", buildCmdExeCommandLine(bin, args)],
188+command: comSpec ?? env?.ComSpec ?? env?.COMSPEC ?? process.env.ComSpec ?? "cmd.exe",
189+shell: false,
190+windowsVerbatimArguments: true,
191+};
192+}
193+194+return {
195+ args,
196+command: bin,
197+ shell,
198+ windowsVerbatimArguments,
199+};
200+}
201+105202function signalNumberFor(signal) {
106203switch (signal) {
107204case "SIGHUP":
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。