Skip to content

Commit d6380bb

Browse files
fix: provide useful error on unknown command
1 parent d885426 commit d6380bb

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

packages/webpack-cli/lib/webpack-cli.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ class WebpackCLI {
273273
},
274274
];
275275

276+
const knownCommands = [bundleCommandOptions, versionCommandOptions, helpCommandOptions, ...externalBuiltInCommandsInfo];
277+
const isKnownCommand = (name) => knownCommands.find((command) => command.name === name || command.alias === name);
278+
276279
const getCommandNameAndOptions = (args) => {
277280
let commandName;
278281
const options = [];
@@ -687,7 +690,20 @@ class WebpackCLI {
687690
await outputVersion(optionsForVersion, program);
688691
}
689692

690-
await loadCommandByName(commandName, true);
693+
if (isKnownCommand(commandName)) {
694+
await loadCommandByName(commandName, true);
695+
} else {
696+
logger.error(`Unknown command '${commandName}'`);
697+
698+
const found = knownCommands.find((commandOptions) => distance(commandName, commandOptions.name) < 3);
699+
700+
if (found) {
701+
logger.error(`Did you mean '${found.name}' (alias '${found.alias}')?`);
702+
}
703+
704+
logger.error("Run 'webpack --help' to see available commands and options");
705+
process.exit(2);
706+
}
691707

692708
await this.program.parseAsync([commandName, ...options], { from: 'user' });
693709
});

test/unknown/unknown.test.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,22 @@ describe('unknown behaviour', () => {
184184
expect(stdout).toBeFalsy();
185185
});
186186

187-
it('should ask to install command if an unknown command passed', () => {
187+
it('should log error if an unknown command passed', () => {
188188
const { exitCode, stderr, stdout } = run(__dirname, ['qqq'], true, [], { TERM_PROGRAM: false });
189189

190-
expect(exitCode).toBe(0);
191-
expect(stripAnsi(stderr)).toContain("For using this command you need to install: 'qqq' package");
192-
expect(stripAnsi(stderr)).toContain("Would you like to install 'qqq' package? (That will run 'npm install -D qqq')");
190+
expect(exitCode).toBe(2);
191+
expect(stripAnsi(stderr)).toContain("Unknown command 'qqq'");
192+
expect(stripAnsi(stderr)).toContain("Run 'webpack --help' to see available commands and options");
193+
expect(stdout).toBeFalsy();
194+
});
195+
196+
it('should log error and provide suggestion if an unknown command passed', () => {
197+
const { exitCode, stderr, stdout } = run(__dirname, ['server'], true, [], { TERM_PROGRAM: false });
198+
199+
expect(exitCode).toBe(2);
200+
expect(stripAnsi(stderr)).toContain("Unknown command 'server'");
201+
expect(stripAnsi(stderr)).toContain("Did you mean 'serve' (alias 's')?");
202+
expect(stripAnsi(stderr)).toContain("Run 'webpack --help' to see available commands and options");
193203
expect(stdout).toBeFalsy();
194204
});
195205
});

0 commit comments

Comments
 (0)