refactor: share cli command registration policy

This commit is contained in:
Peter Steinberger
2026-04-06 13:51:37 +01:00
parent a21709d041
commit 5edabf4776
6 changed files with 102 additions and 127 deletions

View File

@@ -1,5 +1,6 @@
import type { Command } from "commander";
import { getPrimaryCommand, hasHelpOrVersion } from "../argv.js";
import { getPrimaryCommand } from "../argv.js";
import { shouldRegisterPrimaryCommandOnly } from "../command-registration-policy.js";
import { removeCommandByName } from "./command-tree.js";
import type { ProgramContext } from "./context.js";
import {
@@ -28,13 +29,6 @@ type CoreCliEntry = {
register: (params: CommandRegisterParams) => Promise<void> | void;
};
const shouldRegisterCorePrimaryOnly = (argv: string[]) => {
if (hasHelpOrVersion(argv)) {
return false;
}
return true;
};
// Note for humans and agents:
// If you update the list of commands, also check whether they have subcommands
// and set the flag accordingly.
@@ -259,7 +253,7 @@ export async function registerCoreCliByName(
export function registerCoreCliCommands(program: Command, ctx: ProgramContext, argv: string[]) {
const primary = getPrimaryCommand(argv);
if (primary && shouldRegisterCorePrimaryOnly(argv)) {
if (primary && shouldRegisterPrimaryCommandOnly(argv)) {
const entry = coreEntries.find((candidate) =>
candidate.commands.some((cmd) => cmd.name === primary),
);

View File

@@ -1,7 +1,10 @@
import type { Command } from "commander";
import type { OpenClawConfig } from "../../config/config.js";
import { isTruthyEnvValue } from "../../infra/env.js";
import { getPrimaryCommand, hasHelpOrVersion } from "../argv.js";
import { getPrimaryCommand } from "../argv.js";
import {
shouldEagerRegisterSubcommands,
shouldRegisterPrimarySubcommandOnly,
} from "../command-registration-policy.js";
import { removeCommandByName } from "./command-tree.js";
import { registerLazyCommand as registerLazyCommandPlaceholder } from "./register-lazy-command.js";
import {
@@ -18,20 +21,6 @@ type SubCliEntry = SubCliDescriptor & {
register: SubCliRegistrar;
};
const shouldRegisterPrimaryOnly = (argv: string[]) => {
if (isTruthyEnvValue(process.env.OPENCLAW_DISABLE_LAZY_SUBCOMMANDS)) {
return false;
}
if (hasHelpOrVersion(argv)) {
return false;
}
return true;
};
const shouldEagerRegisterSubcommands = (_argv: string[]) => {
return isTruthyEnvValue(process.env.OPENCLAW_DISABLE_LAZY_SUBCOMMANDS);
};
export const loadValidatedConfigForPluginRegistration =
async (): Promise<OpenClawConfig | null> => {
const mod = await import("../../config/config.js");
@@ -348,14 +337,14 @@ function registerLazyCommand(program: Command, entry: SubCliEntry) {
}
export function registerSubCliCommands(program: Command, argv: string[] = process.argv) {
if (shouldEagerRegisterSubcommands(argv)) {
if (shouldEagerRegisterSubcommands()) {
for (const entry of entries) {
void entry.register(program);
}
return;
}
const primary = getPrimaryCommand(argv);
if (primary && shouldRegisterPrimaryOnly(argv)) {
if (primary && shouldRegisterPrimarySubcommandOnly(argv)) {
const entry = entries.find((candidate) => candidate.name === primary);
if (entry) {
registerLazyCommand(program, entry);