refactor(daemon): extract windows cmd argv helpers

This commit is contained in:
Peter Steinberger
2026-02-19 16:22:17 +01:00
parent a1cb700a05
commit 035832b4c5
4 changed files with 81 additions and 35 deletions

26
src/daemon/cmd-argv.ts Normal file
View File

@@ -0,0 +1,26 @@
import { splitArgsPreservingQuotes } from "./arg-split.js";
import { assertNoCmdLineBreak } from "./cmd-set.js";
export function quoteCmdScriptArg(value: string): string {
assertNoCmdLineBreak(value, "Command argument");
if (!value) {
return '""';
}
const escaped = value.replace(/"/g, '\\"').replace(/%/g, "%%").replace(/!/g, "^!");
if (!/[ \t"&|<>^()%!]/g.test(value)) {
return escaped;
}
return `"${escaped}"`;
}
export function unescapeCmdScriptArg(value: string): string {
return value.replace(/\^!/g, "!").replace(/%%/g, "%");
}
export function parseCmdScriptCommandLine(value: string): string[] {
// Script renderer escapes quotes (`\"`) and cmd expansions (`%%`, `^!`).
// Keep all other backslashes literal so Windows drive/UNC paths survive.
return splitArgsPreservingQuotes(value, { escapeMode: "backslash-quote-only" }).map(
unescapeCmdScriptArg,
);
}