Skip to content

Commit 06cbe46

Browse files
refactor: resolve TODO with help and options (#4680)
1 parent 0bbddbf commit 06cbe46

File tree

2 files changed

+80
-79
lines changed

2 files changed

+80
-79
lines changed

packages/webpack-cli/src/webpack-cli.ts

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ interface CommandOption {
122122
describe?: string;
123123
negatedDescription?: string;
124124
defaultValue?: string;
125-
// TODO search API
126-
helpLevel: "minimum" | "verbose";
127125
hidden?: boolean;
128126
group?: "core";
129127
}
@@ -743,7 +741,7 @@ class WebpackCLI {
743741
})
744742
.default(mainOption.defaultValue);
745743

746-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
744+
optionForCommand.hidden = option.hidden || false;
747745

748746
command.addOption(optionForCommand);
749747
} else if (mainOption.type.has(String)) {
@@ -760,23 +758,23 @@ class WebpackCLI {
760758
})
761759
.default(mainOption.defaultValue);
762760

763-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
761+
optionForCommand.hidden = option.hidden || false;
764762

765763
command.addOption(optionForCommand);
766764
} else if (mainOption.type.has(Boolean)) {
767765
const optionForCommand = new Option(mainOption.flags, mainOption.description).default(
768766
mainOption.defaultValue,
769767
);
770768

771-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
769+
optionForCommand.hidden = option.hidden || false;
772770

773771
command.addOption(optionForCommand);
774772
} else {
775773
const optionForCommand = new Option(mainOption.flags, mainOption.description)
776774
.argParser([...mainOption.type][0] as (value: string, previous: unknown) => unknown)
777775
.default(mainOption.defaultValue);
778776

779-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
777+
optionForCommand.hidden = option.hidden || false;
780778

781779
command.addOption(optionForCommand);
782780
}
@@ -806,23 +804,25 @@ class WebpackCLI {
806804
})
807805
.default(mainOption.defaultValue);
808806

809-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
807+
optionForCommand.hidden = option.hidden || false;
810808

811809
command.addOption(optionForCommand);
812810
} else if (mainOption.type.size === 0 && negativeOption) {
813811
const optionForCommand = new Option(mainOption.flags, mainOption.description);
814812

815813
// Hide stub option
816-
optionForCommand.hideHelp();
817-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
814+
optionForCommand.hidden = option.hidden || false;
815+
(optionForCommand as Option & { internal?: boolean }).internal = true;
818816

819817
command.addOption(optionForCommand);
820818
}
821819

822820
if (negativeOption) {
823-
const optionForCommand = new Option(negativeOption.flags, negativeOption.description);
821+
const optionForCommand = new Option(negativeOption.flags, negativeOption.description).default(
822+
false,
823+
);
824824

825-
(optionForCommand as Option & { helpLevel: string }).helpLevel = option.helpLevel;
825+
optionForCommand.hidden = option.hidden || false;
826826

827827
command.addOption(optionForCommand);
828828
}
@@ -847,7 +847,7 @@ class WebpackCLI {
847847
valueName: "pathToConfigFile",
848848
description:
849849
'Provide path to one or more webpack configuration files to process, e.g. "./webpack.config.js".',
850-
helpLevel: "minimum",
850+
hidden: false,
851851
},
852852
{
853853
name: "config-name",
@@ -860,7 +860,7 @@ class WebpackCLI {
860860
valueName: "name",
861861
description:
862862
"Name(s) of particular configuration(s) to use if configuration file exports an array of multiple configurations.",
863-
helpLevel: "minimum",
863+
hidden: false,
864864
},
865865
{
866866
name: "merge",
@@ -872,18 +872,7 @@ class WebpackCLI {
872872
},
873873
],
874874
description: "Merge two or more configurations using 'webpack-merge'.",
875-
helpLevel: "minimum",
876-
},
877-
{
878-
name: "disable-interpret",
879-
configs: [
880-
{
881-
type: "enum",
882-
values: [true],
883-
},
884-
],
885-
description: "Disable interpret for loading the config file.",
886-
helpLevel: "minimum",
875+
hidden: false,
887876
},
888877
// Complex configs
889878
{
@@ -928,7 +917,7 @@ class WebpackCLI {
928917
multiple: true,
929918
description:
930919
'Environment variables passed to the configuration when it is a function, e.g. "myvar" or "myvar=myval".',
931-
helpLevel: "minimum",
920+
hidden: false,
932921
},
933922
{
934923
name: "config-node-env",
@@ -940,7 +929,7 @@ class WebpackCLI {
940929
multiple: false,
941930
description:
942931
"Sets process.env.NODE_ENV to the specified value for access within the configuration.",
943-
helpLevel: "minimum",
932+
hidden: false,
944933
},
945934

946935
// Adding more plugins
@@ -954,7 +943,7 @@ class WebpackCLI {
954943
],
955944
multiple: false,
956945
description: "It invokes webpack-bundle-analyzer plugin to get bundle information.",
957-
helpLevel: "minimum",
946+
hidden: false,
958947
},
959948
{
960949
name: "progress",
@@ -968,7 +957,7 @@ class WebpackCLI {
968957
},
969958
],
970959
description: "Print compilation progress during build.",
971-
helpLevel: "minimum",
960+
hidden: false,
972961
},
973962

974963
// Output options
@@ -986,7 +975,7 @@ class WebpackCLI {
986975
alias: "j",
987976
valueName: "pathToJsonFile",
988977
description: "Prints result as JSON or store it in a file.",
989-
helpLevel: "minimum",
978+
hidden: false,
990979
},
991980
{
992981
name: "fail-on-warnings",
@@ -997,7 +986,18 @@ class WebpackCLI {
997986
},
998987
],
999988
description: "Stop webpack-cli process with non-zero exit code on warnings from webpack.",
1000-
helpLevel: "minimum",
989+
hidden: false,
990+
},
991+
{
992+
name: "disable-interpret",
993+
configs: [
994+
{
995+
type: "enum",
996+
values: [true],
997+
},
998+
],
999+
description: "Disable interpret for loading the config file.",
1000+
hidden: false,
10011001
},
10021002
];
10031003

@@ -1037,7 +1037,7 @@ class WebpackCLI {
10371037
name,
10381038
description: meta.description,
10391039
group: "core",
1040-
helpLevel: minHelpSet.has(name) ? "minimum" : "verbose",
1040+
hidden: !minHelpSet.has(name),
10411041
};
10421042
}
10431043

@@ -1173,7 +1173,7 @@ class WebpackCLI {
11731173
},
11741174
visibleOptions: function visibleOptions(command) {
11751175
return command.options.filter((option) => {
1176-
if (option.hidden) {
1176+
if ((option as Option & { internal?: boolean }).internal) {
11771177
return false;
11781178
}
11791179

@@ -1185,13 +1185,11 @@ class WebpackCLI {
11851185
return false;
11861186
}
11871187

1188-
switch ((option as unknown as CommandOption).helpLevel) {
1189-
case "verbose":
1190-
return isVerbose;
1191-
case "minimum":
1192-
default:
1193-
return true;
1188+
if (option.hidden) {
1189+
return isVerbose;
11941190
}
1191+
1192+
return true;
11951193
});
11961194
},
11971195
padWidth(command, helper: Help) {
@@ -1760,7 +1758,7 @@ class WebpackCLI {
17601758
} else if (this.#isCommand(commandName, WebpackCLI.#commands.version)) {
17611759
await this.makeCommand(
17621760
WebpackCLI.#commands.version,
1763-
() => [
1761+
[
17641762
{
17651763
name: "output",
17661764
alias: "o",
@@ -1770,7 +1768,7 @@ class WebpackCLI {
17701768
},
17711769
],
17721770
description: "To get the output in a specified format (accept json or markdown)",
1773-
helpLevel: "minimum",
1771+
hidden: false,
17741772
},
17751773
],
17761774
async (options: { output?: string }) => {
@@ -1791,7 +1789,7 @@ class WebpackCLI {
17911789
} else if (this.#isCommand(commandName, WebpackCLI.#commands.info)) {
17921790
await this.makeCommand(
17931791
WebpackCLI.#commands.info,
1794-
() => [
1792+
[
17951793
{
17961794
name: "output",
17971795
alias: "o",
@@ -1801,15 +1799,15 @@ class WebpackCLI {
18011799
},
18021800
],
18031801
description: "To get the output in a specified format (accept json or markdown)",
1804-
helpLevel: "minimum",
1802+
hidden: false,
18051803
},
18061804
{
18071805
name: "additional-package",
18081806
alias: "a",
18091807
configs: [{ type: "string" }],
18101808
multiple: true,
18111809
description: "Adds additional packages to the output",
1812-
helpLevel: "minimum",
1810+
hidden: false,
18131811
},
18141812
],
18151813
async (options: { output?: string; additionalPackage?: string[] }) => {
@@ -1977,7 +1975,10 @@ class WebpackCLI {
19771975
const { distance } = require("fastest-levenshtein");
19781976

19791977
for (const option of (command as Command).options) {
1980-
if (!option.hidden && distance(name, option.long?.slice(2) as string) < 3) {
1978+
if (
1979+
!(option as Option & { internal?: boolean }).internal &&
1980+
distance(name, option.long?.slice(2) as string) < 3
1981+
) {
19811982
this.logger.error(`Did you mean '--${option.name()}'?`);
19821983
}
19831984
}

0 commit comments

Comments
 (0)