mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 01:00:43 +00:00
refactor: cache repeated lazy imports
This commit is contained in:
@@ -9,6 +9,8 @@ import { runCommandWithRuntime } from "./cli-utils.js";
|
||||
import { hasExplicitOptions } from "./command-options.js";
|
||||
import { formatHelpExamples } from "./help-format.js";
|
||||
|
||||
type ChannelsCommandsModule = typeof import("../commands/channels.js");
|
||||
|
||||
const optionNamesAdd = [
|
||||
"channel",
|
||||
"account",
|
||||
@@ -49,6 +51,13 @@ const optionNamesAdd = [
|
||||
|
||||
const optionNamesRemove = ["channel", "account", "delete"] as const;
|
||||
|
||||
let channelsCommandsPromise: Promise<ChannelsCommandsModule> | undefined;
|
||||
|
||||
function loadChannelsCommands(): Promise<ChannelsCommandsModule> {
|
||||
channelsCommandsPromise ??= import("../commands/channels.js");
|
||||
return channelsCommandsPromise;
|
||||
}
|
||||
|
||||
function runChannelsCommand(action: () => Promise<void>) {
|
||||
return runCommandWithRuntime(defaultRuntime, action);
|
||||
}
|
||||
@@ -89,7 +98,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsListCommand } = await import("../commands/channels.js");
|
||||
const { channelsListCommand } = await loadChannelsCommands();
|
||||
await channelsListCommand(opts, defaultRuntime);
|
||||
});
|
||||
});
|
||||
@@ -102,7 +111,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsStatusCommand } = await import("../commands/channels.js");
|
||||
const { channelsStatusCommand } = await loadChannelsCommands();
|
||||
await channelsStatusCommand(opts, defaultRuntime);
|
||||
});
|
||||
});
|
||||
@@ -117,7 +126,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsCapabilitiesCommand } = await import("../commands/channels.js");
|
||||
const { channelsCapabilitiesCommand } = await loadChannelsCommands();
|
||||
await channelsCapabilitiesCommand(opts, defaultRuntime);
|
||||
});
|
||||
});
|
||||
@@ -132,7 +141,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (entries, opts) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsResolveCommand } = await import("../commands/channels.js");
|
||||
const { channelsResolveCommand } = await loadChannelsCommands();
|
||||
await channelsResolveCommand(
|
||||
{
|
||||
channel: opts.channel as string | undefined,
|
||||
@@ -154,7 +163,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--json", "Output JSON", false)
|
||||
.action(async (opts) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsLogsCommand } = await import("../commands/channels.js");
|
||||
const { channelsLogsCommand } = await loadChannelsCommands();
|
||||
await channelsLogsCommand(opts, defaultRuntime);
|
||||
});
|
||||
});
|
||||
@@ -200,7 +209,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--use-env", "Use env token (default account only)", false)
|
||||
.action(async (opts, command) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsAddCommand } = await import("../commands/channels.js");
|
||||
const { channelsAddCommand } = await loadChannelsCommands();
|
||||
const hasFlags = hasExplicitOptions(command, optionNamesAdd);
|
||||
await channelsAddCommand(opts, defaultRuntime, { hasFlags });
|
||||
});
|
||||
@@ -214,7 +223,7 @@ export function registerChannelsCli(program: Command) {
|
||||
.option("--delete", "Delete config entries (no prompt)", false)
|
||||
.action(async (opts, command) => {
|
||||
await runChannelsCommand(async () => {
|
||||
const { channelsRemoveCommand } = await import("../commands/channels.js");
|
||||
const { channelsRemoveCommand } = await loadChannelsCommands();
|
||||
const hasFlags = hasExplicitOptions(command, optionNamesRemove);
|
||||
await channelsRemoveCommand(opts, defaultRuntime, { hasFlags });
|
||||
});
|
||||
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
type RouteArgParser<TArgs> = (argv: string[]) => TArgs | null;
|
||||
|
||||
type ParsedRouteArgs<TParse extends RouteArgParser<unknown>> = Exclude<ReturnType<TParse>, null>;
|
||||
type ConfigCliModule = typeof import("../config-cli.js");
|
||||
type ModelsCommandsModule = typeof import("../../commands/models.js");
|
||||
|
||||
export type RoutedCommandDefinition<TParse extends RouteArgParser<unknown>> = {
|
||||
parseArgs: TParse;
|
||||
@@ -31,6 +33,19 @@ function defineRoutedCommand<TParse extends RouteArgParser<unknown>>(
|
||||
return definition;
|
||||
}
|
||||
|
||||
let configCliPromise: Promise<ConfigCliModule> | undefined;
|
||||
let modelsCommandsPromise: Promise<ModelsCommandsModule> | undefined;
|
||||
|
||||
function loadConfigCli(): Promise<ConfigCliModule> {
|
||||
configCliPromise ??= import("../config-cli.js");
|
||||
return configCliPromise;
|
||||
}
|
||||
|
||||
function loadModelsCommands(): Promise<ModelsCommandsModule> {
|
||||
modelsCommandsPromise ??= import("../../commands/models.js");
|
||||
return modelsCommandsPromise;
|
||||
}
|
||||
|
||||
export const routedCommandDefinitions = {
|
||||
health: defineRoutedCommand({
|
||||
parseArgs: parseHealthRouteArgs,
|
||||
@@ -83,28 +98,28 @@ export const routedCommandDefinitions = {
|
||||
"config-get": defineRoutedCommand({
|
||||
parseArgs: parseConfigGetRouteArgs,
|
||||
runParsedArgs: async (args) => {
|
||||
const { runConfigGet } = await import("../config-cli.js");
|
||||
const { runConfigGet } = await loadConfigCli();
|
||||
await runConfigGet(args);
|
||||
},
|
||||
}),
|
||||
"config-unset": defineRoutedCommand({
|
||||
parseArgs: parseConfigUnsetRouteArgs,
|
||||
runParsedArgs: async (args) => {
|
||||
const { runConfigUnset } = await import("../config-cli.js");
|
||||
const { runConfigUnset } = await loadConfigCli();
|
||||
await runConfigUnset(args);
|
||||
},
|
||||
}),
|
||||
"models-list": defineRoutedCommand({
|
||||
parseArgs: parseModelsListRouteArgs,
|
||||
runParsedArgs: async (args) => {
|
||||
const { modelsListCommand } = await import("../../commands/models.js");
|
||||
const { modelsListCommand } = await loadModelsCommands();
|
||||
await modelsListCommand(args, defaultRuntime);
|
||||
},
|
||||
}),
|
||||
"models-status": defineRoutedCommand({
|
||||
parseArgs: parseModelsStatusRouteArgs,
|
||||
runParsedArgs: async (args) => {
|
||||
const { modelsStatusCommand } = await import("../../commands/models.js");
|
||||
const { modelsStatusCommand } = await loadModelsCommands();
|
||||
await modelsStatusCommand(args, defaultRuntime);
|
||||
},
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user