refactor(daemon): share quoted arg splitter

This commit is contained in:
Peter Steinberger
2026-02-15 12:49:30 +00:00
parent 216f4d4669
commit 108ea4336b
4 changed files with 91 additions and 62 deletions

View File

@@ -1,6 +1,7 @@
import fs from "node:fs/promises";
import path from "node:path";
import type { GatewayServiceRuntime } from "./service-runtime.js";
import { splitArgsPreservingQuotes } from "./arg-split.js";
import { formatGatewayServiceDescription, resolveGatewayWindowsTaskName } from "./constants.js";
import { formatLine } from "./output.js";
import { resolveGatewayStateDir } from "./paths.js";
@@ -48,36 +49,9 @@ function resolveTaskUser(env: Record<string, string | undefined>): string | null
}
function parseCommandLine(value: string): string[] {
const args: string[] = [];
let current = "";
let inQuotes = false;
for (let i = 0; i < value.length; i++) {
const char = value[i];
// `buildTaskScript` only escapes quotes (`\"`).
// Keep all other backslashes literal so drive and UNC paths are preserved.
if (char === "\\" && i + 1 < value.length && value[i + 1] === '"') {
current += value[i + 1];
i++;
continue;
}
if (char === '"') {
inQuotes = !inQuotes;
continue;
}
if (!inQuotes && /\s/.test(char)) {
if (current) {
args.push(current);
current = "";
}
continue;
}
current += char;
}
if (current) {
args.push(current);
}
return args;
// `buildTaskScript` only escapes quotes (`\"`).
// Keep all other backslashes literal so drive and UNC paths are preserved.
return splitArgsPreservingQuotes(value, { escapeMode: "backslash-quote-only" });
}
export async function readScheduledTaskCommand(env: Record<string, string | undefined>): Promise<{