mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-12 17:51:22 +00:00
73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import type { Command } from "commander";
|
|
import { loadConfig, readConfigFileSnapshot, type OpenClawConfig } from "../config/config.js";
|
|
import {
|
|
createPluginCliLogger,
|
|
loadPluginCliDescriptors,
|
|
loadPluginCliRegistrationEntriesWithDefaults,
|
|
type PluginCliLoaderOptions,
|
|
} from "./cli-registry-loader.js";
|
|
import { registerPluginCliCommandGroups } from "./register-plugin-cli-command-groups.js";
|
|
import type { OpenClawPluginCliCommandDescriptor } from "./types.js";
|
|
|
|
type PluginCliRegistrationMode = "eager" | "lazy";
|
|
|
|
type RegisterPluginCliOptions = {
|
|
mode?: PluginCliRegistrationMode;
|
|
primary?: string | null;
|
|
};
|
|
|
|
const logger = createPluginCliLogger();
|
|
|
|
export const loadValidatedConfigForPluginRegistration =
|
|
async (): Promise<OpenClawConfig | null> => {
|
|
const snapshot = await readConfigFileSnapshot();
|
|
if (!snapshot.valid) {
|
|
return null;
|
|
}
|
|
return loadConfig();
|
|
};
|
|
|
|
export async function getPluginCliCommandDescriptors(
|
|
cfg?: OpenClawConfig,
|
|
env?: NodeJS.ProcessEnv,
|
|
loaderOptions?: PluginCliLoaderOptions,
|
|
): Promise<OpenClawPluginCliCommandDescriptor[]> {
|
|
return loadPluginCliDescriptors({ cfg, env, loaderOptions });
|
|
}
|
|
|
|
export async function registerPluginCliCommands(
|
|
program: Command,
|
|
cfg?: OpenClawConfig,
|
|
env?: NodeJS.ProcessEnv,
|
|
loaderOptions?: PluginCliLoaderOptions,
|
|
options?: RegisterPluginCliOptions,
|
|
) {
|
|
const mode = options?.mode ?? "eager";
|
|
const primary = options?.primary ?? null;
|
|
|
|
await registerPluginCliCommandGroups(
|
|
program,
|
|
await loadPluginCliRegistrationEntriesWithDefaults({ cfg, env, loaderOptions }),
|
|
{
|
|
mode,
|
|
primary,
|
|
existingCommands: new Set(program.commands.map((cmd) => cmd.name())),
|
|
logger,
|
|
},
|
|
);
|
|
}
|
|
|
|
export async function registerPluginCliCommandsFromValidatedConfig(
|
|
program: Command,
|
|
env?: NodeJS.ProcessEnv,
|
|
loaderOptions?: PluginCliLoaderOptions,
|
|
options?: RegisterPluginCliOptions,
|
|
): Promise<OpenClawConfig | null> {
|
|
const config = await loadValidatedConfigForPluginRegistration();
|
|
if (!config) {
|
|
return null;
|
|
}
|
|
await registerPluginCliCommands(program, config, env, loaderOptions, options);
|
|
return config;
|
|
}
|