Files
openclaw/src/cli/cron-cli/output-mode.ts
T
Peter Steinberger 6205cf3bb3 fix(cron): stop advertising inactive JSON defaults (#124903)
* fix(cron): make --json help match behavior

* fix(cron): preserve scratch write JSON output

* test(cli): classify cron JSON result commands
2026-08-16 17:32:49 -07:00

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);
}