Skip to content

Commit

Permalink
refactor: improve suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Oct 26, 2022
1 parent c45aadd commit 7f28546
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 11 additions & 0 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,14 @@ class WebpackCLI implements IWebpackCLI {
this.logger.error("Run 'webpack --help' to see available commands and options");
process.exit(2);
}

const levenshtein = require("fastest-levenshtein");

(command as WebpackCLICommand).options.forEach((option) => {
if (!option.hidden && levenshtein.distance(name, option.long?.slice(2)) < 3) {
this.logger.error(`Did you mean '--${option.name()}'?`);
}
});
}
}
}
Expand Down Expand Up @@ -1287,6 +1295,9 @@ class WebpackCLI implements IWebpackCLI {
"Output the version number of 'webpack', 'webpack-cli' and 'webpack-dev-server' and commands.",
);

// webpack-cli has it's own logic for showing suggestions
this.program.showSuggestionAfterError(false);

const outputHelp = async (
options: string[],
isVerbose: boolean,
Expand Down
15 changes: 8 additions & 7 deletions test/build/unknown/__snapshots__/unknown.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,56 @@ exports[`unknown behaviour should log an error if an unknown flag is passed and

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag #2: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--output-fileneme'
(Did you mean --output-filename?)
[webpack-cli] Did you mean '--output-filename'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag #2: stdout 1`] = `""`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag #3: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--output-library-auxiliary-comment-commnjs'
(Did you mean --output-library-auxiliary-comment-commonjs?)
[webpack-cli] Did you mean '--output-library-auxiliary-comment-commonjs'?
[webpack-cli] Did you mean '--output-library-auxiliary-comment-commonjs2'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag #3: stdout 1`] = `""`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "b" command: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--entyr'
(Did you mean --entry?)
[webpack-cli] Did you mean '--entry'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "b" command: stdout 1`] = `""`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "bundle" command: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--entyr'
(Did you mean --entry?)
[webpack-cli] Did you mean '--entry'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "bundle" command: stdout 1`] = `""`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "i" command: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--outpyt'
(Did you mean --output?)
[webpack-cli] Did you mean '--output'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "i" command: stdout 1`] = `""`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "info" command: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--outpyt'
(Did you mean --output?)
[webpack-cli] Did you mean '--output'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag using "info" command: stdout 1`] = `""`;

exports[`unknown behaviour should log an error if an unknown flag is passed and suggests the closest match to an unknown flag: stderr 1`] = `
"[webpack-cli] Error: Unknown option '--entyr'
(Did you mean --entry?)
[webpack-cli] Did you mean '--entry'?
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

Expand Down

0 comments on commit 7f28546

Please sign in to comment.