Skip to content

Commit

Permalink
breaking: Fix ability to override commands via config (#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
huntie authored Jul 18, 2023
1 parent be7e6e2 commit ba362e6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ function attachCommand<C extends Command<boolean>>(
command: C,
config: C extends DetachedCommand ? Config | undefined : Config,
): void {
// commander@9.x will internally push commands into an array structure!
// Commands with duplicate names (e.g. from config) must be reduced before
// calling this function.
// https://unpkg.com/browse/commander@9.4.1/lib/command.js#L1308
if (program.commands.find((cmd) => cmd.name() === command.name)) {
throw new Error(
'Invariant Violation: Attempted to override an already registered ' +
`command: '${command.name}'. This is not supported by the underlying ` +
'library and will cause bugs. Ensure a command with this `name` is ' +
'only registered once.',
);
}

const cmd = program
.command(command.name)
.option('--verbose', 'Increase logging verbosity')
Expand Down Expand Up @@ -165,7 +178,14 @@ async function setupAndRun() {

logger.enable();

const commands: Record<string, Command> = {};

// Reduce overridden commands before registering
for (const command of [...projectCommands, ...config.commands]) {
commands[command.name] = command;
}

for (const command of Object.values(commands)) {
attachCommand(command, config);
}
} catch (error) {
Expand Down

0 comments on commit ba362e6

Please sign in to comment.