// Pure command codec shared by the CLI and structured schedule editors. /** * shell 风格 token 化:引号在 token **任意位置**出现即进入引号模式。 * * 自 legacy 移植。`--input topic="带空格的值"` 里引号在 `=` 之后、token 开头是 `t`, * 只认「开头引号」的写法会把值截断并留下引号残留 —— 这是实测过的。 */ function tokenizeArgs(s: string): string[] { const out: string[] = []; let i = 0; while (i < s.length) { while (i < s.length && /\s/.test(s[i])) i++; if (i >= s.length) break; let acc = ''; while (i < s.length) { const c = s[i]; if (c === '"' || c === "'") { const q = c; i++; while (i < s.length && s[i] !== q) acc += s[i++]; if (i < s.length) i++; } else if (/\s/.test(c)) { break; } else { acc += c; i++; } } out.push(acc); } return out; } export function parseWorkflowArgs(rawArgs: string): { name: string | null; inputs: Record; positional: string[]; } { const tokens = tokenizeArgs(rawArgs); const inputs: Record = {}; const positional: string[] = []; let name: string | null = null; for (let i = 0; i < tokens.length; i++) { const t = tokens[i]; if (t === '--input') { const kv = tokens[i + 1]; if (!kv) break; const eq = kv.indexOf('='); if (eq > 0) inputs[kv.slice(0, eq)] = kv.slice(eq + 1); i++; continue; } if (t.startsWith('--')) { i++; // 未知 flag:保守跳过其后一个 token(认为它带值) continue; } if (name === null) name = t.replace(/\.ya?ml$/i, ''); else positional.push(t); } return { name, inputs, positional }; } /** Quote one token for tokenizeArgs (not a shell execution API). */ export function quoteWorkflowToken(value: string): string { return "'" + value.replaceAll("'", "'\"'\"'") + "'"; } export function formatWorkflowCommand( name: string, inputs: Record, ): string { return [ quoteWorkflowToken(name), ...Object.entries(inputs).flatMap(([key, value]) => [ '--input', quoteWorkflowToken(key + '=' + value), ]), ].join(' '); }