mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-17 08:02:12 -06:00
6205cf3bb3
* fix(cron): make --json help match behavior * fix(cron): preserve scratch write JSON output * test(cli): classify cron JSON result commands
55 lines
1.9 KiB
TypeScript
55 lines
1.9 KiB
TypeScript
import type { Command } from "commander";
|
|
import {
|
|
getMachineOutputCommandPath,
|
|
MACHINE_OUTPUT_JSON_OPTION_DESCRIPTION,
|
|
} from "../machine-output-argv.js";
|
|
|
|
const CRON_SCRATCH_JSON_OPTION_DESCRIPTION =
|
|
"Output scratch plus revision metadata as JSON; writes return JSON by default";
|
|
|
|
type CronOutputCommandDefinition = {
|
|
aliases: readonly string[];
|
|
alwaysJson: boolean;
|
|
};
|
|
|
|
const CRON_OUTPUT_COMMANDS = {
|
|
status: { aliases: [], alwaysJson: true },
|
|
add: { aliases: ["create"], alwaysJson: true },
|
|
rm: { aliases: ["remove", "delete"], alwaysJson: true },
|
|
enable: { aliases: [], alwaysJson: true },
|
|
disable: { aliases: [], alwaysJson: true },
|
|
get: { aliases: [], alwaysJson: true },
|
|
runs: { aliases: [], alwaysJson: true },
|
|
run: { aliases: [], alwaysJson: true },
|
|
edit: { aliases: [], alwaysJson: true },
|
|
scratch: { aliases: [], alwaysJson: false },
|
|
} as const satisfies Record<string, CronOutputCommandDefinition>;
|
|
|
|
type CronOutputCommandName = keyof typeof CRON_OUTPUT_COMMANDS;
|
|
const MACHINE_OUTPUT_COMMANDS = new Set<string>();
|
|
for (const [name, definition] of Object.entries(CRON_OUTPUT_COMMANDS)) {
|
|
MACHINE_OUTPUT_COMMANDS.add(name);
|
|
for (const alias of definition.aliases) {
|
|
MACHINE_OUTPUT_COMMANDS.add(alias);
|
|
}
|
|
}
|
|
|
|
export function createCronOutputCommand(parent: Command, name: CronOutputCommandName): Command {
|
|
const definition = CRON_OUTPUT_COMMANDS[name];
|
|
const command = parent.command(name);
|
|
for (const alias of definition.aliases) {
|
|
command.alias(alias);
|
|
}
|
|
return definition.alwaysJson
|
|
? command.option("--json", MACHINE_OUTPUT_JSON_OPTION_DESCRIPTION)
|
|
: command.option("--json", CRON_SCRATCH_JSON_OPTION_DESCRIPTION);
|
|
}
|
|
|
|
export function isCronMachineOutput(argv: readonly string[]): boolean {
|
|
const [, command] = getMachineOutputCommandPath(argv, 2);
|
|
if (!command) {
|
|
return false;
|
|
}
|
|
return MACHINE_OUTPUT_COMMANDS.has(command);
|
|
}
|