refactor(utils): share shell argv tokenizer

This commit is contained in:
Peter Steinberger
2026-02-14 14:54:20 +00:00
parent 8218a94a31
commit e1e05e57cb
3 changed files with 33 additions and 70 deletions

View File

@@ -1,3 +1,9 @@
const DOUBLE_QUOTE_ESCAPES = new Set(["\\", '"', "$", "`", "\n", "\r"]);
function isDoubleQuoteEscape(next: string | undefined): next is string {
return Boolean(next && DOUBLE_QUOTE_ESCAPES.has(next));
}
export function splitShellArgs(raw: string): string[] | null {
const tokens: string[] = [];
let buf = "";
@@ -32,6 +38,12 @@ export function splitShellArgs(raw: string): string[] | null {
continue;
}
if (inDouble) {
const next = raw[i + 1];
if (ch === "\\" && isDoubleQuoteEscape(next)) {
buf += next;
i += 1;
continue;
}
if (ch === '"') {
inDouble = false;
} else {