Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update commander to v9 #3460

Merged
merged 4 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/webpack-cli/package.json
Expand Up @@ -39,7 +39,7 @@
"@webpack-cli/info": "^1.5.0",
"@webpack-cli/serve": "^1.7.0",
"colorette": "^2.0.14",
"commander": "^7.0.0",
"commander": "^9.4.1",
"cross-spawn": "^7.0.3",
"envinfo": "^7.7.3",
"fastest-levenshtein": "^1.0.12",
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/src/types.ts
Expand Up @@ -19,7 +19,7 @@ import webpack, {
import { ClientConfiguration, Configuration as DevServerConfig } from "webpack-dev-server";

import { Colorette } from "colorette";
import { Command, CommandOptions, OptionConstructor, ParseOptions } from "commander";
import { Command, CommandOptions, Option, ParseOptions } from "commander";
import { prepare } from "rechoir";
import { stringifyStream } from "@discoveryjs/json-ext";

Expand Down Expand Up @@ -278,7 +278,7 @@ interface ImportLoaderError extends Error {
/**
* External libraries types
*/

type OptionConstructor = new (flags: string, description?: string) => Option;
type CommanderOption = InstanceType<OptionConstructor>;

interface Rechoir {
Expand Down
17 changes: 6 additions & 11 deletions packages/webpack-cli/src/webpack-cli.ts
Expand Up @@ -479,7 +479,10 @@ class WebpackCLI implements IWebpackCLI {
}) as WebpackCLICommand;

if (commandOptions.description) {
command.description(commandOptions.description, commandOptions.argsDescription);
command.description(
commandOptions.description,
commandOptions.argsDescription as { [argName: string]: string },
);
}

if (commandOptions.usage) {
Expand Down Expand Up @@ -1245,14 +1248,6 @@ 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 @@ -1335,7 +1330,7 @@ class WebpackCLI implements IWebpackCLI {
// Support multiple aliases
subcommandTerm: (command: WebpackCLICommand) => {
const humanReadableArgumentName = (argument: WebpackCLICommandOption) => {
const nameOutput = argument.name + (argument.variadic === true ? "..." : "");
const nameOutput = argument.name() + (argument.variadic === true ? "..." : "");

return argument.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
};
Expand Down Expand Up @@ -1412,7 +1407,7 @@ class WebpackCLI implements IWebpackCLI {
// Arguments
const argumentList = helper
.visibleArguments(command)
.map((argument) => formatItem(argument.term, argument.description));
.map((argument) => formatItem(argument.name(), argument.description));

if (argumentList.length > 0) {
output = output.concat([bold("Arguments:"), formatList(argumentList), ""]);
Expand Down
2 changes: 1 addition & 1 deletion test/api/CLI.test.js
Expand Up @@ -1340,7 +1340,7 @@ describe("CLI API", () => {
],
(options) => {
expect(options).toEqual({
booleanAndNumberAndString: "default",
booleanAndNumberAndString: true,
});
},
);
Expand Down
15 changes: 7 additions & 8 deletions test/build/unknown/__snapshots__/unknown.test.js.snap.webpack5
Expand Up @@ -37,56 +37,55 @@ 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'
[webpack-cli] Did you mean '--output-filename'?
(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'
[webpack-cli] Did you mean '--output-library-auxiliary-comment-commonjs'?
[webpack-cli] Did you mean '--output-library-auxiliary-comment-commonjs2'?
(Did you mean --output-library-auxiliary-comment-commonjs?)
[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'
[webpack-cli] Did you mean '--entry'?
(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'
[webpack-cli] Did you mean '--entry'?
(Did you mean --entry?)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be great to have [webpack-cli] prefix here, is there the places where we can hook?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we can disable it with this.program.showSuggestionAfterError(false); and keep our own logic.

[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'
[webpack-cli] Did you mean '--output'?
(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'
[webpack-cli] Did you mean '--output'?
(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'
[webpack-cli] Did you mean '--entry'?
(Did you mean --entry?)
[webpack-cli] Run 'webpack --help' to see available commands and options"
`;

Expand Down
9 changes: 2 additions & 7 deletions yarn.lock
Expand Up @@ -4020,17 +4020,12 @@ commander@^2.20.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==

commander@^7.0.0, commander@^7.2.0:
commander@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==

commander@^9.0.0, commander@^9.3.0:
version "9.4.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.4.1.tgz#d1dd8f2ce6faf93147295c0df13c7c21141cfbdd"
integrity sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==

commander@^9.4.0:
commander@^9.0.0, commander@^9.3.0, commander@^9.4.0, commander@^9.4.1:
version "9.4.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.4.1.tgz#d1dd8f2ce6faf93147295c0df13c7c21141cfbdd"
integrity sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==
Expand Down