Skip to content

Commit ba362e6

Browse files
authored
breaking: Fix ability to override commands via config (#1999)
1 parent be7e6e2 commit ba362e6

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

packages/cli/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ function attachCommand<C extends Command<boolean>>(
8282
command: C,
8383
config: C extends DetachedCommand ? Config | undefined : Config,
8484
): void {
85+
// [email protected] will internally push commands into an array structure!
86+
// Commands with duplicate names (e.g. from config) must be reduced before
87+
// calling this function.
88+
// https://unpkg.com/browse/[email protected]/lib/command.js#L1308
89+
if (program.commands.find((cmd) => cmd.name() === command.name)) {
90+
throw new Error(
91+
'Invariant Violation: Attempted to override an already registered ' +
92+
`command: '${command.name}'. This is not supported by the underlying ` +
93+
'library and will cause bugs. Ensure a command with this `name` is ' +
94+
'only registered once.',
95+
);
96+
}
97+
8598
const cmd = program
8699
.command(command.name)
87100
.option('--verbose', 'Increase logging verbosity')
@@ -165,7 +178,14 @@ async function setupAndRun() {
165178

166179
logger.enable();
167180

181+
const commands: Record<string, Command> = {};
182+
183+
// Reduce overridden commands before registering
168184
for (const command of [...projectCommands, ...config.commands]) {
185+
commands[command.name] = command;
186+
}
187+
188+
for (const command of Object.values(commands)) {
169189
attachCommand(command, config);
170190
}
171191
} catch (error) {

0 commit comments

Comments
 (0)