refactor(cli): dedupe service-load and command-removal loops

This commit is contained in:
Peter Steinberger
2026-02-18 22:40:09 +00:00
parent 5e76cefc70
commit c7458782b8
2 changed files with 44 additions and 31 deletions

View File

@@ -237,6 +237,17 @@ function removeCommand(program: Command, command: Command) {
}
}
function removeEntryCommands(program: Command, entry: CoreCliEntry) {
// 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);
}
}
}
function registerLazyCoreCommand(
program: Command,
ctx: ProgramContext,
@@ -247,14 +258,7 @@ function registerLazyCoreCommand(
placeholder.allowUnknownOption(true);
placeholder.allowExcessArguments(true);
placeholder.action(async (...actionArgs) => {
// 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);
}
}
removeEntryCommands(program, entry);
await entry.register({ program, ctx, argv: process.argv });
await reparseProgramFromActionArgs(program, actionArgs);
});
@@ -273,14 +277,7 @@ export async function registerCoreCliByName(
return false;
}
// 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);
}
}
removeEntryCommands(program, entry);
await entry.register({ program, ctx, argv });
return true;
}