From 889f00f9a309b10fec0da1ad6ac30e31e9cde6ed Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Wed, 5 Jul 2023 11:02:52 +0100 Subject: [PATCH] breaking: Fix ability to override commands via config --- packages/cli/src/index.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 222e479e2d..33d07f8e5a 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -82,6 +82,19 @@ function attachCommand>( 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 + // @ts-ignore + if (program._findCommand(command.name)) { + throw new Error( + 'Invariant Violation: Attempted to override an already registered ' + + 'command. 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') @@ -165,7 +178,14 @@ async function setupAndRun() { logger.enable(); + const commands: Record = {}; + + // 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) {