|
1 | 1 | import { spawnSync } from "node:child_process"; |
2 | 2 | import process from "node:process"; |
3 | 3 |
|
| 4 | +const WINDOWS_CMD_META_CHARACTERS = /([()\][%!^"`<>&|;, *?])/g; |
| 5 | + |
| 6 | +function readEnvValue(env, name) { |
| 7 | + const matchingKey = Object.keys(env ?? {}).find((key) => key.toUpperCase() === name.toUpperCase()); |
| 8 | + return matchingKey ? env[matchingKey] : undefined; |
| 9 | +} |
| 10 | + |
| 11 | +function quotePosixShellArgument(argument) { |
| 12 | + return `'${String(argument).replaceAll("'", `'\\''`)}'`; |
| 13 | +} |
| 14 | + |
| 15 | +function quoteCmdShellCommand(command) { |
| 16 | + return String(command).replace(WINDOWS_CMD_META_CHARACTERS, "^$1"); |
| 17 | +} |
| 18 | + |
| 19 | +function quoteCmdShellArgument(argument) { |
| 20 | + let quoted = String(argument); |
| 21 | + |
| 22 | + // Preserve quotes and trailing backslashes when cmd.exe hands this argument |
| 23 | + // to the target process, then protect cmd metacharacters from interpretation. |
| 24 | + quoted = quoted.replace(/(\\*)"/g, (_match, backslashes) => `${backslashes}${backslashes}\\"`); |
| 25 | + quoted = quoted.replace(/(\\+)$/, "$1$1"); |
| 26 | + quoted = `"${quoted}"`; |
| 27 | + return quoted.replace(WINDOWS_CMD_META_CHARACTERS, "^$1"); |
| 28 | +} |
| 29 | + |
| 30 | +function isCmdShell(shell) { |
| 31 | + return shell === true || /(?:^|[\\/])cmd(?:\.exe)?$/i.test(String(shell)); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Builds a spawn invocation that preserves argv boundaries when Windows needs |
| 36 | + * a shell to launch command shims such as codex.cmd and npm.cmd. |
| 37 | + */ |
| 38 | +export function prepareSpawnCommand(command, args = [], options = {}) { |
| 39 | + const platform = options.platform ?? process.platform; |
| 40 | + if (platform !== "win32") { |
| 41 | + return { command, args: [...args], shell: false }; |
| 42 | + } |
| 43 | + |
| 44 | + const env = options.env ?? process.env; |
| 45 | + const configuredShell = options.shell ?? readEnvValue(env, "SHELL") ?? readEnvValue(process.env, "SHELL") ?? true; |
| 46 | + if (!configuredShell) { |
| 47 | + return { command, args: [...args], shell: false }; |
| 48 | + } |
| 49 | + |
| 50 | + const commandLine = isCmdShell(configuredShell) |
| 51 | + ? [quoteCmdShellCommand(command), ...args.map(quoteCmdShellArgument)].join(" ") |
| 52 | + : [command, ...args].map(quotePosixShellArgument).join(" "); |
| 53 | + |
| 54 | + // Pass a single, fully quoted command string. Node otherwise joins a command |
| 55 | + // and args with spaces before handing them to the shell, losing argv bounds. |
| 56 | + return { command: commandLine, args: [], shell: configuredShell }; |
| 57 | +} |
| 58 | + |
4 | 59 | export function runCommand(command, args = [], options = {}) { |
5 | | - const result = spawnSync(command, args, { |
| 60 | + const invocation = prepareSpawnCommand(command, args, { env: options.env }); |
| 61 | + const result = spawnSync(invocation.command, invocation.args, { |
6 | 62 | cwd: options.cwd, |
7 | 63 | env: options.env, |
8 | 64 | encoding: "utf8", |
9 | 65 | input: options.input, |
10 | 66 | maxBuffer: options.maxBuffer, |
11 | 67 | stdio: options.stdio ?? "pipe", |
12 | | - shell: process.platform === "win32" ? (process.env.SHELL || true) : false, |
| 68 | + shell: invocation.shell, |
13 | 69 | windowsHide: true |
14 | 70 | }); |
15 | 71 |
|
|
0 commit comments