Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

breaking: Fix ability to override commands via config #1999

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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