* Escapes a single argument for `cmd.exe` shell mode (`spawn(..., { shell: true })` * on Windows). Node joins the command and arguments with spaces and hands the * resulting string to `cmd.exe` without any quoting, so every dynamic argument * must be escaped to survive both cmd.exe parsing and th
(arg: string)
| 58 | * `CommandLineToArgvW` parsing. Mirrors cross-spawn's argument escaping. |
| 59 | */ |
| 60 | function escapeWindowsShellArg(arg: string): string { |
| 61 | // Double up backslashes that precede a double quote, then escape the quote |
| 62 | // itself so it survives CommandLineToArgvW. |
| 63 | let escaped = arg.replace(/(\\*)"/g, '$1$1\\"'); |
| 64 | // Double up trailing backslashes so the closing quote is not escaped away. |
| 65 | escaped = escaped.replace(/(\\*)$/, "$1$1"); |
| 66 | // Quote the whole argument so embedded whitespace is preserved. |
| 67 | escaped = `"${escaped}"`; |
| 68 | // Escape cmd.exe metacharacters so cmd passes them through verbatim. |
| 69 | return escaped.replace(WINDOWS_SHELL_META_CHARS, "^$1"); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Escapes arguments for shell-mode spawns: applies {@link escapeWindowsShellArg} |