File size: 1,460 Bytes
f778c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // Descriptor-to-lazy-command-group adapters used by core and sub-CLI registration.
import type { Command } from "commander";
import type { MachineOutputResolver } from "../machine-output-argv.js";
/** Descriptor for one root command placeholder. */
export type NamedCommandDescriptor = {
name: string;
description: string;
hasSubcommands: boolean;
machineOutput?: MachineOutputResolver;
hidden?: boolean;
parentDefaultHelp?: boolean;
};
/** Command names owned by one lazy registrar. */
export type CommandGroupDescriptorSpec<TArgs extends unknown[] = []> = readonly [
commandNames: readonly string[],
register: (program: Command, ...args: TArgs) => Promise<void> | void,
];
/** Bind descriptors and registration arguments without importing the command modules. */
export function buildCommandGroupEntries<TArgs extends unknown[]>(
descriptors: readonly NamedCommandDescriptor[],
specs: readonly CommandGroupDescriptorSpec<TArgs>[],
...args: TArgs
) {
const descriptorsByName = new Map(descriptors.map((descriptor) => [descriptor.name, descriptor]));
return specs.map(([commandNames, register]) => ({
names: commandNames,
placeholders: commandNames.map((name) => {
const descriptor = descriptorsByName.get(name);
if (!descriptor) {
throw new Error(`Unknown command descriptor: ${name}`);
}
return descriptor;
}),
register: (program: Command) => register(program, ...args),
}));
}
|