Files
openclaw/src/cli/setup-onboard-configure-help-fast-path.ts
Frank Yang 2c0c9c92f4 perf(cli): speed up onboarding help startup (#84488)
Merged via squash.

Prepared head SHA: b3b086e6d8
Co-authored-by: frankekn <4488090+frankekn@users.noreply.github.com>
Co-authored-by: frankekn <4488090+frankekn@users.noreply.github.com>
Reviewed-by: @frankekn
2026-05-21 11:21:58 +08:00

93 lines
2.7 KiB
TypeScript

import { Command } from "commander";
import { VERSION } from "../version.js";
import { resolveCliArgvInvocation } from "./argv-invocation.js";
import type { ProgramContext } from "./program/context.js";
import { configureProgramHelp } from "./program/help.js";
type SetupOnboardConfigureHelpCommand = "setup" | "onboard" | "configure";
const SETUP_ONBOARD_CONFIGURE_HELP_COMMANDS = new Set<SetupOnboardConfigureHelpCommand>([
"setup",
"onboard",
"configure",
]);
function isCommanderParseExit(error: unknown): error is { exitCode: number } {
if (!error || typeof error !== "object") {
return false;
}
const candidate = error as { code?: unknown; exitCode?: unknown };
return (
typeof candidate.exitCode === "number" &&
Number.isInteger(candidate.exitCode) &&
typeof candidate.code === "string" &&
candidate.code.startsWith("commander.")
);
}
function resolveSetupOnboardConfigureHelpCommand(
argv: string[],
): SetupOnboardConfigureHelpCommand | null {
const invocation = resolveCliArgvInvocation(argv);
if (invocation.commandPath.length !== 1 || !invocation.hasHelpOrVersion) {
return null;
}
const command = invocation.commandPath[0];
return SETUP_ONBOARD_CONFIGURE_HELP_COMMANDS.has(command as SetupOnboardConfigureHelpCommand)
? (command as SetupOnboardConfigureHelpCommand)
: null;
}
function createHelpContext(): ProgramContext {
return {
programVersion: VERSION,
channelOptions: [],
messageChannelOptions: "",
agentChannelOptions: "last",
};
}
async function registerHelpCommand(
program: Command,
command: SetupOnboardConfigureHelpCommand,
): Promise<void> {
if (command === "setup") {
const { registerSetupCommand } = await import("./program/register.setup.js");
registerSetupCommand(program);
return;
}
if (command === "onboard") {
const { registerOnboardCommand } = await import("./program/register.onboard.js");
registerOnboardCommand(program);
return;
}
const { registerConfigureCommand } = await import("./program/register.configure.js");
registerConfigureCommand(program);
}
export async function tryOutputSetupOnboardConfigureHelp(argv: string[]): Promise<boolean> {
const command = resolveSetupOnboardConfigureHelpCommand(argv);
if (!command) {
return false;
}
const program = new Command();
program.enablePositionalOptions();
program.exitOverride((err) => {
process.exitCode = typeof err.exitCode === "number" ? err.exitCode : 1;
throw err;
});
configureProgramHelp(program, createHelpContext());
await registerHelpCommand(program, command);
try {
await program.parseAsync(argv);
} catch (error) {
if (!isCommanderParseExit(error)) {
throw error;
}
process.exitCode = error.exitCode;
}
return true;
}