mirror of
https://github.com/whyour/qinglong.git
synced 2026-11-18 00:57:53 +08:00
Reconcile scheduled and system cron triggers, isolate concurrent executions and retain verifiable exit statistics across restarts. Add paged task history, log and script browsing, dashboard timing details and regression coverage.
40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
// Read literal shell words only. Never expand or execute a command to derive a
|
|
// path. Adjacent quoted/unquoted fragments belong to the same argument.
|
|
function words(command: string): string[] {
|
|
const result: string[] = [];
|
|
let value = '', quote = '', active = false;
|
|
for (let i = 0; i < command.length; i++) {
|
|
const c = command[i];
|
|
if (quote && c === quote) { quote = ''; continue; }
|
|
if (!quote && (c === "'" || c === '"')) { quote = c; active = true; continue; }
|
|
if (c === '\\' && quote !== "'") {
|
|
const next = command[++i];
|
|
if (next === undefined) return [];
|
|
if (quote === '"' && !/[$`"\\\n]/.test(next)) value += '\\';
|
|
if (next !== '\n') { value += next; active = true; }
|
|
continue;
|
|
}
|
|
if (!quote && /[;&|<>()\n]/.test(c)) break;
|
|
if (!quote && /\s/.test(c)) {
|
|
if (active) result.push(value);
|
|
value = ''; active = false; continue;
|
|
}
|
|
if (!active && !quote && c === '#') break;
|
|
value += c; active = true;
|
|
}
|
|
if (quote) return [];
|
|
if (active) result.push(value);
|
|
return result;
|
|
}
|
|
|
|
export function cronOperand(command: string, taskOnly = false): string | undefined {
|
|
const args = words(command.trim());
|
|
if (args[0] === 'task') args.shift();
|
|
else if (taskOnly && args[0] === 'ql') return;
|
|
while (args[0] === '-l' || args[0] === '-m') {
|
|
if (args.shift() === '-m') args.shift();
|
|
}
|
|
if (args.length > 1 && /^(?:.*\/)?(?:node|python3?|bash|sh|ts-node(?:-transpile-only)?)$/.test(args[0])) args.shift();
|
|
return args[0];
|
|
}
|