mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-16 20:40:45 +00:00
Add shared parseBooleanValue()/isTruthyEnvValue() and apply across CLI, gateway, memory, and live-test flags for consistent env handling. Introduce route-first fast paths, lazy subcommand registration, and deferred plugin loading to reduce CLI startup overhead. Centralize config validation via ensureConfigReady() and add config caching/deferred shell env fallback for fewer IO passes. Harden logger initialization/imports and add focused tests for argv, boolean parsing, frontmatter, and CLI subcommands.
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
import process from "node:process";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { loadDotEnv } from "../infra/dotenv.js";
|
|
import { normalizeEnv } from "../infra/env.js";
|
|
import { isMainModule } from "../infra/is-main.js";
|
|
import { ensureClawdbotCliOnPath } from "../infra/path-env.js";
|
|
import { assertSupportedRuntime } from "../infra/runtime-guard.js";
|
|
import { installUnhandledRejectionHandler } from "../infra/unhandled-rejections.js";
|
|
import { enableConsoleCapture } from "../logging.js";
|
|
import { hasHelpOrVersion } from "./argv.js";
|
|
import { tryRouteCli } from "./route.js";
|
|
|
|
export function rewriteUpdateFlagArgv(argv: string[]): string[] {
|
|
const index = argv.indexOf("--update");
|
|
if (index === -1) return argv;
|
|
|
|
const next = [...argv];
|
|
next.splice(index, 1, "update");
|
|
return next;
|
|
}
|
|
|
|
export async function runCli(argv: string[] = process.argv) {
|
|
loadDotEnv({ quiet: true });
|
|
normalizeEnv();
|
|
ensureClawdbotCliOnPath();
|
|
|
|
// Capture all console output into structured logs while keeping stdout/stderr behavior.
|
|
enableConsoleCapture();
|
|
|
|
// Enforce the minimum supported runtime before doing any work.
|
|
assertSupportedRuntime();
|
|
|
|
if (await tryRouteCli(argv)) return;
|
|
|
|
const { buildProgram } = await import("./program.js");
|
|
const program = buildProgram();
|
|
|
|
// Global error handlers to prevent silent crashes from unhandled rejections/exceptions.
|
|
// These log the error and exit gracefully instead of crashing without trace.
|
|
installUnhandledRejectionHandler();
|
|
|
|
process.on("uncaughtException", (error) => {
|
|
console.error("[clawdbot] Uncaught exception:", error.stack ?? error.message);
|
|
process.exit(1);
|
|
});
|
|
|
|
await program.parseAsync(rewriteUpdateFlagArgv(argv));
|
|
}
|
|
|
|
export function isCliMainModule(): boolean {
|
|
return isMainModule({ currentFile: fileURLToPath(import.meta.url) });
|
|
}
|