-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking: Fix ability to override commands via config
- Loading branch information
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,20 @@ function attachCommand<C extends Command<boolean>>( | |
command: C, | ||
config: C extends DetachedCommand ? Config | undefined : Config, | ||
): void { | ||
// [email protected] 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/[email protected]/lib/command.js#L1308 | ||
// @ts-ignore | ||
if (program._findCommand(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') | ||
|
@@ -165,7 +179,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) { | ||
|