mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 21:01:43 +03:00
fix(cli): remove grouped placeholders before register
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import type { ProgramContext } from "./context.js";
|
import type { ProgramContext } from "./context.js";
|
||||||
import {
|
import {
|
||||||
getCoreCliCommandNames,
|
getCoreCliCommandNames,
|
||||||
@@ -7,6 +7,14 @@ import {
|
|||||||
registerCoreCliCommands,
|
registerCoreCliCommands,
|
||||||
} from "./command-registry.js";
|
} from "./command-registry.js";
|
||||||
|
|
||||||
|
vi.mock("./register.status-health-sessions.js", () => ({
|
||||||
|
registerStatusHealthSessionsCommands: (program: Command) => {
|
||||||
|
program.command("status");
|
||||||
|
program.command("health");
|
||||||
|
program.command("sessions");
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
const testProgramContext: ProgramContext = {
|
const testProgramContext: ProgramContext = {
|
||||||
programVersion: "0.0.0-test",
|
programVersion: "0.0.0-test",
|
||||||
channelOptions: [],
|
channelOptions: [],
|
||||||
@@ -57,4 +65,23 @@ describe("command-registry", () => {
|
|||||||
expect(names).toContain("uninstall");
|
expect(names).toContain("uninstall");
|
||||||
expect(names).not.toContain("maintenance");
|
expect(names).not.toContain("maintenance");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("registers grouped core entry placeholders without duplicate command errors", async () => {
|
||||||
|
const program = new Command();
|
||||||
|
registerCoreCliCommands(program, testProgramContext, ["node", "openclaw", "vitest"]);
|
||||||
|
|
||||||
|
const prevArgv = process.argv;
|
||||||
|
process.argv = ["node", "openclaw", "status"];
|
||||||
|
try {
|
||||||
|
program.exitOverride();
|
||||||
|
await program.parseAsync(["node", "openclaw", "status"]);
|
||||||
|
} finally {
|
||||||
|
process.argv = prevArgv;
|
||||||
|
}
|
||||||
|
|
||||||
|
const names = program.commands.map((command) => command.name());
|
||||||
|
expect(names).toContain("status");
|
||||||
|
expect(names).toContain("health");
|
||||||
|
expect(names).toContain("sessions");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -148,7 +148,14 @@ function registerLazyCoreCommand(
|
|||||||
placeholder.allowUnknownOption(true);
|
placeholder.allowUnknownOption(true);
|
||||||
placeholder.allowExcessArguments(true);
|
placeholder.allowExcessArguments(true);
|
||||||
placeholder.action(async (...actionArgs) => {
|
placeholder.action(async (...actionArgs) => {
|
||||||
removeCommand(program, placeholder);
|
// Some registrars install multiple top-level commands (e.g. status/health/sessions).
|
||||||
|
// Remove placeholders/old registrations for all names in the entry before re-registering.
|
||||||
|
for (const cmd of entry.commands) {
|
||||||
|
const existing = program.commands.find((c) => c.name() === cmd.name);
|
||||||
|
if (existing) {
|
||||||
|
removeCommand(program, existing);
|
||||||
|
}
|
||||||
|
}
|
||||||
await entry.register({ program, ctx, argv: process.argv });
|
await entry.register({ program, ctx, argv: process.argv });
|
||||||
const actionCommand = actionArgs.at(-1) as Command | undefined;
|
const actionCommand = actionArgs.at(-1) as Command | undefined;
|
||||||
const root = actionCommand?.parent ?? program;
|
const root = actionCommand?.parent ?? program;
|
||||||
|
|||||||
Reference in New Issue
Block a user