fix: quiet update banner and skip duplicate plugin CLI
This commit is contained in:
@@ -4,6 +4,7 @@ import { emitCliBanner } from "../banner.js";
|
|||||||
import { getCommandPath, getVerboseFlag, hasHelpOrVersion } from "../argv.js";
|
import { getCommandPath, getVerboseFlag, hasHelpOrVersion } from "../argv.js";
|
||||||
import { ensureConfigReady } from "./config-guard.js";
|
import { ensureConfigReady } from "./config-guard.js";
|
||||||
import { ensurePluginRegistryLoaded } from "../plugin-registry.js";
|
import { ensurePluginRegistryLoaded } from "../plugin-registry.js";
|
||||||
|
import { isTruthyEnvValue } from "../../infra/env.js";
|
||||||
import { setVerbose } from "../../globals.js";
|
import { setVerbose } from "../../globals.js";
|
||||||
|
|
||||||
function setProcessTitleForCommand(actionCommand: Command) {
|
function setProcessTitleForCommand(actionCommand: Command) {
|
||||||
@@ -22,15 +23,21 @@ const PLUGIN_REQUIRED_COMMANDS = new Set(["message", "channels", "directory"]);
|
|||||||
export function registerPreActionHooks(program: Command, programVersion: string) {
|
export function registerPreActionHooks(program: Command, programVersion: string) {
|
||||||
program.hook("preAction", async (_thisCommand, actionCommand) => {
|
program.hook("preAction", async (_thisCommand, actionCommand) => {
|
||||||
setProcessTitleForCommand(actionCommand);
|
setProcessTitleForCommand(actionCommand);
|
||||||
emitCliBanner(programVersion);
|
|
||||||
const argv = process.argv;
|
const argv = process.argv;
|
||||||
if (hasHelpOrVersion(argv)) return;
|
if (hasHelpOrVersion(argv)) return;
|
||||||
|
const commandPath = getCommandPath(argv, 2);
|
||||||
|
const hideBanner =
|
||||||
|
isTruthyEnvValue(process.env.CLAWDBOT_HIDE_BANNER) ||
|
||||||
|
commandPath[0] === "update" ||
|
||||||
|
(commandPath[0] === "plugins" && commandPath[1] === "update");
|
||||||
|
if (!hideBanner) {
|
||||||
|
emitCliBanner(programVersion);
|
||||||
|
}
|
||||||
const verbose = getVerboseFlag(argv, { includeDebug: true });
|
const verbose = getVerboseFlag(argv, { includeDebug: true });
|
||||||
setVerbose(verbose);
|
setVerbose(verbose);
|
||||||
if (!verbose) {
|
if (!verbose) {
|
||||||
process.env.NODE_NO_WARNINGS ??= "1";
|
process.env.NODE_NO_WARNINGS ??= "1";
|
||||||
}
|
}
|
||||||
const commandPath = getCommandPath(argv, 2);
|
|
||||||
if (commandPath[0] === "doctor") return;
|
if (commandPath[0] === "doctor") return;
|
||||||
await ensureConfigReady({ runtime: defaultRuntime, commandPath });
|
await ensureConfigReady({ runtime: defaultRuntime, commandPath });
|
||||||
// Load plugins for commands that need channel access
|
// Load plugins for commands that need channel access
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import {
|
|||||||
type UpdateStepInfo,
|
type UpdateStepInfo,
|
||||||
type UpdateStepResult,
|
type UpdateStepResult,
|
||||||
type UpdateStepProgress,
|
type UpdateStepProgress,
|
||||||
type UpdateStepResult,
|
|
||||||
} from "../infra/update-runner.js";
|
} from "../infra/update-runner.js";
|
||||||
import {
|
import {
|
||||||
detectGlobalInstallManagerByPresence,
|
detectGlobalInstallManagerByPresence,
|
||||||
|
|||||||
45
src/plugins/cli.test.ts
Normal file
45
src/plugins/cli.test.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { Command } from "commander";
|
||||||
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
const mocks = vi.hoisted(() => ({
|
||||||
|
memoryRegister: vi.fn(),
|
||||||
|
otherRegister: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("./loader.js", () => ({
|
||||||
|
loadClawdbotPlugins: () => ({
|
||||||
|
cliRegistrars: [
|
||||||
|
{
|
||||||
|
pluginId: "memory-core",
|
||||||
|
register: mocks.memoryRegister,
|
||||||
|
commands: ["memory"],
|
||||||
|
source: "bundled",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pluginId: "other",
|
||||||
|
register: mocks.otherRegister,
|
||||||
|
commands: ["other"],
|
||||||
|
source: "bundled",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
|
||||||
|
import { registerPluginCliCommands } from "./cli.js";
|
||||||
|
|
||||||
|
describe("registerPluginCliCommands", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
mocks.memoryRegister.mockClear();
|
||||||
|
mocks.otherRegister.mockClear();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips plugin CLI registrars when commands already exist", () => {
|
||||||
|
const program = new Command();
|
||||||
|
program.command("memory");
|
||||||
|
|
||||||
|
registerPluginCliCommands(program, {} as any);
|
||||||
|
|
||||||
|
expect(mocks.memoryRegister).not.toHaveBeenCalled();
|
||||||
|
expect(mocks.otherRegister).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -24,7 +24,20 @@ export function registerPluginCliCommands(program: Command, cfg?: ClawdbotConfig
|
|||||||
logger,
|
logger,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const existingCommands = new Set(program.commands.map((cmd) => cmd.name()));
|
||||||
|
|
||||||
for (const entry of registry.cliRegistrars) {
|
for (const entry of registry.cliRegistrars) {
|
||||||
|
if (entry.commands.length > 0) {
|
||||||
|
const overlaps = entry.commands.filter((command) => existingCommands.has(command));
|
||||||
|
if (overlaps.length > 0) {
|
||||||
|
log.debug(
|
||||||
|
`plugin CLI register skipped (${entry.pluginId}): command already registered (${overlaps.join(
|
||||||
|
", ",
|
||||||
|
)})`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const result = entry.register({
|
const result = entry.register({
|
||||||
program,
|
program,
|
||||||
@@ -37,6 +50,9 @@ export function registerPluginCliCommands(program: Command, cfg?: ClawdbotConfig
|
|||||||
log.warn(`plugin CLI register failed (${entry.pluginId}): ${String(err)}`);
|
log.warn(`plugin CLI register failed (${entry.pluginId}): ${String(err)}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
for (const command of entry.commands) {
|
||||||
|
existingCommands.add(command);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn(`plugin CLI register failed (${entry.pluginId}): ${String(err)}`);
|
log.warn(`plugin CLI register failed (${entry.pluginId}): ${String(err)}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user