From d6380bb6c6756d2a00ac20f2ffc454481d97e4d3 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 31 Dec 2020 19:47:43 +0300 Subject: [PATCH] fix: provide useful error on unknown command --- packages/webpack-cli/lib/webpack-cli.js | 18 +++++++++++++++++- test/unknown/unknown.test.js | 18 ++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index c3b91fc554e..2f85a0c85ee 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -273,6 +273,9 @@ class WebpackCLI { }, ]; + const knownCommands = [bundleCommandOptions, versionCommandOptions, helpCommandOptions, ...externalBuiltInCommandsInfo]; + const isKnownCommand = (name) => knownCommands.find((command) => command.name === name || command.alias === name); + const getCommandNameAndOptions = (args) => { let commandName; const options = []; @@ -687,7 +690,20 @@ class WebpackCLI { await outputVersion(optionsForVersion, program); } - await loadCommandByName(commandName, true); + if (isKnownCommand(commandName)) { + await loadCommandByName(commandName, true); + } else { + logger.error(`Unknown command '${commandName}'`); + + const found = knownCommands.find((commandOptions) => distance(commandName, commandOptions.name) < 3); + + if (found) { + logger.error(`Did you mean '${found.name}' (alias '${found.alias}')?`); + } + + logger.error("Run 'webpack --help' to see available commands and options"); + process.exit(2); + } await this.program.parseAsync([commandName, ...options], { from: 'user' }); }); diff --git a/test/unknown/unknown.test.js b/test/unknown/unknown.test.js index bb8ba841b06..78258b22580 100644 --- a/test/unknown/unknown.test.js +++ b/test/unknown/unknown.test.js @@ -184,12 +184,22 @@ describe('unknown behaviour', () => { expect(stdout).toBeFalsy(); }); - it('should ask to install command if an unknown command passed', () => { + it('should log error if an unknown command passed', () => { const { exitCode, stderr, stdout } = run(__dirname, ['qqq'], true, [], { TERM_PROGRAM: false }); - expect(exitCode).toBe(0); - expect(stripAnsi(stderr)).toContain("For using this command you need to install: 'qqq' package"); - expect(stripAnsi(stderr)).toContain("Would you like to install 'qqq' package? (That will run 'npm install -D qqq')"); + expect(exitCode).toBe(2); + expect(stripAnsi(stderr)).toContain("Unknown command 'qqq'"); + expect(stripAnsi(stderr)).toContain("Run 'webpack --help' to see available commands and options"); + expect(stdout).toBeFalsy(); + }); + + it('should log error and provide suggestion if an unknown command passed', () => { + const { exitCode, stderr, stdout } = run(__dirname, ['server'], true, [], { TERM_PROGRAM: false }); + + expect(exitCode).toBe(2); + expect(stripAnsi(stderr)).toContain("Unknown command 'server'"); + expect(stripAnsi(stderr)).toContain("Did you mean 'serve' (alias 's')?"); + expect(stripAnsi(stderr)).toContain("Run 'webpack --help' to see available commands and options"); expect(stdout).toBeFalsy(); }); });