MCPcopy Create free account
hub / github.com/pingdotgg/t3code / escapeWindowsShellArg

Function escapeWindowsShellArg

packages/shared/src/shell.ts:60–70  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

58 * `CommandLineToArgvW` parsing. Mirrors cross-spawn's argument escaping.
59 */
60function 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}

Callers 1

shell.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected