mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-27 21:07:01 -06:00
refactor(cli): trim internal command exports (#106426)
This commit is contained in:
committed by
GitHub
parent
0ff265838b
commit
a965d28e04
@@ -5,9 +5,6 @@ import {
|
||||
addCommandDescriptorsToProgram,
|
||||
collectUniqueCommandDescriptors,
|
||||
defineCommandDescriptorCatalog,
|
||||
getCommandDescriptorNames,
|
||||
getCommandsWithSubcommands,
|
||||
getParentDefaultHelpCommands,
|
||||
} from "./command-descriptor-utils.js";
|
||||
|
||||
describe("command-descriptor-utils", () => {
|
||||
@@ -17,18 +14,6 @@ describe("command-descriptor-utils", () => {
|
||||
{ name: "gamma", description: "Gamma", hasSubcommands: true, parentDefaultHelp: true },
|
||||
] as const;
|
||||
|
||||
it("returns descriptor names in order", () => {
|
||||
expect(getCommandDescriptorNames(descriptors)).toEqual(["alpha", "beta", "gamma"]);
|
||||
});
|
||||
|
||||
it("returns commands with subcommands", () => {
|
||||
expect(getCommandsWithSubcommands(descriptors)).toEqual(["beta", "gamma"]);
|
||||
});
|
||||
|
||||
it("returns commands with parent default help", () => {
|
||||
expect(getParentDefaultHelpCommands(descriptors)).toEqual(["gamma"]);
|
||||
});
|
||||
|
||||
it("collects unique descriptors across groups in order", () => {
|
||||
expect(
|
||||
collectUniqueCommandDescriptors([
|
||||
|
||||
@@ -37,23 +37,19 @@ export function sanitizeCommandDescriptorDescription(description: string): strin
|
||||
}
|
||||
|
||||
/** Return descriptor names in registration order. */
|
||||
export function getCommandDescriptorNames(descriptors: readonly CommandDescriptorLike[]): string[] {
|
||||
function getCommandDescriptorNames(descriptors: readonly CommandDescriptorLike[]): string[] {
|
||||
return descriptors.map((descriptor) => descriptor.name);
|
||||
}
|
||||
|
||||
/** Return descriptor names that should remain parent commands with subcommands. */
|
||||
export function getCommandsWithSubcommands(
|
||||
descriptors: readonly NamedCommandDescriptor[],
|
||||
): string[] {
|
||||
function getCommandsWithSubcommands(descriptors: readonly NamedCommandDescriptor[]): string[] {
|
||||
return descriptors
|
||||
.filter((descriptor) => descriptor.hasSubcommands)
|
||||
.map((descriptor) => descriptor.name);
|
||||
}
|
||||
|
||||
/** Return descriptors whose parent command should show help by default. */
|
||||
export function getParentDefaultHelpCommands(
|
||||
descriptors: readonly NamedCommandDescriptor[],
|
||||
): string[] {
|
||||
function getParentDefaultHelpCommands(descriptors: readonly NamedCommandDescriptor[]): string[] {
|
||||
return descriptors
|
||||
.filter((descriptor) => descriptor.parentDefaultHelp)
|
||||
.map((descriptor) => descriptor.name);
|
||||
|
||||
@@ -6,7 +6,6 @@ import {
|
||||
buildCommandGroupEntries,
|
||||
defineImportedCommandGroupSpec,
|
||||
defineImportedProgramCommandGroupSpecs,
|
||||
resolveCommandGroupEntries,
|
||||
} from "./command-group-descriptors.js";
|
||||
|
||||
const descriptors = [
|
||||
@@ -23,18 +22,6 @@ const descriptors = [
|
||||
] as const;
|
||||
|
||||
describe("command-group-descriptors", () => {
|
||||
it("resolves placeholders by descriptor name", () => {
|
||||
const register = vi.fn();
|
||||
expect(
|
||||
resolveCommandGroupEntries(descriptors, [{ commandNames: ["alpha"], register }]),
|
||||
).toEqual([
|
||||
{
|
||||
placeholders: [descriptors[0]],
|
||||
register,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("builds command-group entries with a register mapper", () => {
|
||||
const register = vi.fn();
|
||||
const mappedRegister = vi.fn();
|
||||
|
||||
@@ -33,7 +33,7 @@ function buildDescriptorIndex<TDescriptor extends NamedCommandDescriptor>(
|
||||
}
|
||||
|
||||
/** Resolve named command-group specs into descriptor-backed entries. */
|
||||
export function resolveCommandGroupEntries<TDescriptor extends NamedCommandDescriptor, TRegister>(
|
||||
function resolveCommandGroupEntries<TDescriptor extends NamedCommandDescriptor, TRegister>(
|
||||
descriptors: readonly TDescriptor[],
|
||||
specs: readonly CommandGroupDescriptorSpec<TRegister>[],
|
||||
): ResolvedCommandGroupEntry<TDescriptor, TRegister>[] {
|
||||
|
||||
@@ -12,7 +12,6 @@ import type { ProgramContext } from "./context.js";
|
||||
import {
|
||||
getCoreCliCommandDescriptors,
|
||||
getCoreCliCommandNames as getCoreDescriptorNames,
|
||||
getCoreCliCommandsWithSubcommands,
|
||||
} from "./core-command-descriptors.js";
|
||||
import {
|
||||
registerCommandGroupByName,
|
||||
@@ -20,8 +19,6 @@ import {
|
||||
type CommandGroupEntry,
|
||||
} from "./register-command-groups.js";
|
||||
|
||||
export { getCoreCliCommandsWithSubcommands };
|
||||
|
||||
type CommandRegisterParams = {
|
||||
program: Command;
|
||||
ctx: ProgramContext;
|
||||
|
||||
@@ -52,10 +52,10 @@ vi.mock("./register.crestodian.js", () => ({
|
||||
|
||||
import {
|
||||
getCoreCliCommandNames,
|
||||
getCoreCliCommandsWithSubcommands,
|
||||
registerCoreCliByName,
|
||||
registerCoreCliCommands,
|
||||
} from "./command-registry.js";
|
||||
} from "./command-registry-core.js";
|
||||
import { getCoreCliCommandsWithSubcommands } from "./core-command-descriptors.js";
|
||||
|
||||
const testProgramContext: ProgramContext = {
|
||||
programVersion: "0.0.0-test",
|
||||
|
||||
@@ -1,20 +1,10 @@
|
||||
// Program command registry facade: exports core descriptors and registers core plus sub-CLIs.
|
||||
import type { Command } from "commander";
|
||||
import {
|
||||
getCoreCliCommandNames,
|
||||
getCoreCliCommandsWithSubcommands,
|
||||
registerCoreCliByName,
|
||||
registerCoreCliCommands,
|
||||
} from "./command-registry-core.js";
|
||||
import { registerCoreCliCommands } from "./command-registry-core.js";
|
||||
import type { ProgramContext } from "./context.js";
|
||||
import { registerSubCliCommands } from "./register.subclis.js";
|
||||
|
||||
export {
|
||||
getCoreCliCommandNames,
|
||||
getCoreCliCommandsWithSubcommands,
|
||||
registerCoreCliByName,
|
||||
registerCoreCliCommands,
|
||||
};
|
||||
export { registerCoreCliByName } from "./command-registry-core.js";
|
||||
|
||||
/** Register all root-program commands for the current argv shape. */
|
||||
export function registerProgramCommands(
|
||||
|
||||
@@ -1,26 +1,9 @@
|
||||
// Command tree tests cover CLI command hierarchy construction and lookup.
|
||||
import { Command } from "commander";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { removeCommand, removeCommandByName } from "./command-tree.js";
|
||||
import { removeCommandByName } from "./command-tree.js";
|
||||
|
||||
describe("command-tree", () => {
|
||||
it("removes a command instance when present", () => {
|
||||
const program = new Command();
|
||||
const alpha = program.command("alpha");
|
||||
program.command("beta");
|
||||
|
||||
expect(removeCommand(program, alpha)).toBe(true);
|
||||
expect(program.commands.map((command) => command.name())).toEqual(["beta"]);
|
||||
});
|
||||
|
||||
it("returns false when command instance is already absent", () => {
|
||||
const program = new Command();
|
||||
program.command("alpha");
|
||||
const detached = new Command("beta");
|
||||
|
||||
expect(removeCommand(program, detached)).toBe(false);
|
||||
});
|
||||
|
||||
it("removes by command name", () => {
|
||||
const program = new Command();
|
||||
program.command("alpha");
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import type { Command } from "commander";
|
||||
|
||||
/** Remove an exact Command instance from a parent program. */
|
||||
export function removeCommand(program: Command, command: Command): boolean {
|
||||
function removeCommand(program: Command, command: Command): boolean {
|
||||
const commands = program.commands as Command[];
|
||||
const index = commands.indexOf(command);
|
||||
if (index < 0) {
|
||||
|
||||
@@ -20,10 +20,7 @@ import {
|
||||
registerSubCliCommands as registerSubCliCommandsCore,
|
||||
type SubCliRegistrationContext,
|
||||
} from "./register.subclis-core.js";
|
||||
import {
|
||||
getSubCliEntries as getSubCliEntryDescriptors,
|
||||
type SubCliDescriptor,
|
||||
} from "./subcli-descriptors.js";
|
||||
import { getSubCliEntries as getSubCliEntryDescriptors } from "./subcli-descriptors.js";
|
||||
|
||||
type SubCliRegistrar = (
|
||||
program: Command,
|
||||
@@ -54,11 +51,6 @@ function resolveSubCliCommandGroups(
|
||||
);
|
||||
}
|
||||
|
||||
/** Return visible sub-CLI descriptors after private QA filtering. */
|
||||
export function getSubCliEntries(): ReadonlyArray<SubCliDescriptor> {
|
||||
return getSubCliEntryDescriptors();
|
||||
}
|
||||
|
||||
/** Register one sub-CLI by name, including lazy command groups. */
|
||||
export async function registerSubCliByName(
|
||||
program: Command,
|
||||
|
||||
@@ -5,7 +5,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getCoreCliCommandNames, registerCoreCliByName } from "./command-registry-core.js";
|
||||
import { createProgramContext } from "./context.js";
|
||||
import { getCoreCliCommandDescriptors } from "./core-command-descriptors.js";
|
||||
import { getSubCliEntries, registerSubCliByName } from "./register.subclis.js";
|
||||
import { registerSubCliByName } from "./register.subclis.js";
|
||||
import { getSubCliEntries } from "./subcli-descriptors.js";
|
||||
|
||||
describe("root command descriptions", () => {
|
||||
beforeEach(() => {
|
||||
|
||||
Reference in New Issue
Block a user