Skip to content

Commit 3b0f74f

Browse files
refactor: reduce loops (#3744)
1 parent fb3671a commit 3b0f74f

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

packages/generators/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class GeneratorsCommand {
2626
configs: [{ type: "string" }],
2727
description: "Type of template",
2828
defaultValue: "default",
29+
helpLevel: "minimum",
2930
},
3031
{
3132
name: "force",
@@ -37,6 +38,7 @@ class GeneratorsCommand {
3738
},
3839
],
3940
description: "Generate without questions (ideally) using default answers",
41+
helpLevel: "minimum",
4042
},
4143
],
4244
async (generationPath: string, options: InitOptions) => {
@@ -76,6 +78,7 @@ class GeneratorsCommand {
7678
configs: [{ type: "string" }],
7779
description: "Type of template",
7880
defaultValue: "default",
81+
helpLevel: "minimum",
7982
},
8083
],
8184
async (outputPath: string, options: LoaderOptions) => {
@@ -115,6 +118,7 @@ class GeneratorsCommand {
115118
configs: [{ type: "string" }],
116119
description: "Type of template",
117120
defaultValue: "default",
121+
helpLevel: "minimum",
118122
},
119123
],
120124
async (outputPath: string, options: PluginOptions) => {

packages/webpack-cli/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,12 @@ interface WebpackCLIBuiltInFlag {
144144
describe?: string;
145145
negatedDescription?: string;
146146
defaultValue?: string;
147+
helpLevel: "minimum" | "verbose";
147148
}
148149

149150
interface WebpackCLIBuiltInOption extends WebpackCLIBuiltInFlag {
150151
hidden?: boolean;
151152
group?: "core";
152-
helpLevel?: "minimum" | "verbose";
153153
}
154154

155155
type WebpackCLIExternalCommandInfo = Pick<WebpackCLIOptions, "name" | "alias" | "description"> & {

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

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,15 @@ class WebpackCLI implements IWebpackCLI {
388388
},
389389
],
390390
description: "To get the output in a specified format ( accept json or markdown )",
391+
helpLevel: "minimum",
391392
},
392393
{
393394
name: "additional-package",
394395
alias: "a",
395396
configs: [{ type: "string" }],
396397
multiple: true,
397398
description: "Adds additional packages to the output",
399+
helpLevel: "minimum",
398400
},
399401
];
400402
}
@@ -810,6 +812,7 @@ class WebpackCLI implements IWebpackCLI {
810812
],
811813
multiple: true,
812814
description: "Provide path to a webpack configuration file e.g. ./webpack.config.js.",
815+
helpLevel: "minimum",
813816
},
814817
{
815818
name: "config-name",
@@ -820,6 +823,7 @@ class WebpackCLI implements IWebpackCLI {
820823
],
821824
multiple: true,
822825
description: "Name of the configuration to use.",
826+
helpLevel: "minimum",
823827
},
824828
{
825829
name: "merge",
@@ -831,6 +835,7 @@ class WebpackCLI implements IWebpackCLI {
831835
},
832836
],
833837
description: "Merge two or more configurations using 'webpack-merge'.",
838+
helpLevel: "minimum",
834839
},
835840
{
836841
name: "disable-interpret",
@@ -841,6 +846,7 @@ class WebpackCLI implements IWebpackCLI {
841846
},
842847
],
843848
description: "Disable interpret for loading the config file.",
849+
helpLevel: "minimum",
844850
},
845851
// Complex configs
846852
{
@@ -888,6 +894,7 @@ class WebpackCLI implements IWebpackCLI {
888894
},
889895
multiple: true,
890896
description: "Environment passed to the configuration when it is a function.",
897+
helpLevel: "minimum",
891898
},
892899
{
893900
name: "node-env",
@@ -898,6 +905,7 @@ class WebpackCLI implements IWebpackCLI {
898905
],
899906
multiple: false,
900907
description: "Sets process.env.NODE_ENV to the specified value.",
908+
helpLevel: "minimum",
901909
},
902910
{
903911
name: "define-process-env-node-env",
@@ -909,6 +917,7 @@ class WebpackCLI implements IWebpackCLI {
909917
multiple: false,
910918
description:
911919
"Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)",
920+
helpLevel: "minimum",
912921
},
913922

914923
// Adding more plugins
@@ -922,6 +931,7 @@ class WebpackCLI implements IWebpackCLI {
922931
],
923932
multiple: false,
924933
description: "It invokes webpack-bundle-analyzer plugin to get bundle information.",
934+
helpLevel: "minimum",
925935
},
926936
{
927937
name: "progress",
@@ -935,6 +945,7 @@ class WebpackCLI implements IWebpackCLI {
935945
},
936946
],
937947
description: "Print compilation progress during build.",
948+
helpLevel: "minimum",
938949
},
939950

940951
// Output options
@@ -951,6 +962,7 @@ class WebpackCLI implements IWebpackCLI {
951962
],
952963
alias: "j",
953964
description: "Prints result as JSON or store it in a file.",
965+
helpLevel: "minimum",
954966
},
955967
{
956968
name: "fail-on-warnings",
@@ -961,11 +973,11 @@ class WebpackCLI implements IWebpackCLI {
961973
},
962974
],
963975
description: "Stop webpack-cli process with non-zero exit code on warnings from webpack",
976+
helpLevel: "minimum",
964977
},
965978
];
966979

967980
const minimumHelpFlags = [
968-
...builtInFlags.map((flag) => flag.name),
969981
"mode",
970982
"watch",
971983
"watch-options-stdin",
@@ -979,37 +991,18 @@ class WebpackCLI implements IWebpackCLI {
979991

980992
// Extract all the flags being exported from core.
981993
// A list of cli flags generated by core can be found here https://github.com/webpack/webpack/blob/main/test/__snapshots__/Cli.basictest.js.snap
982-
const coreArguments = Object.entries(this.webpack.cli.getArguments()).map(([flag, meta]) => {
983-
const inBuiltIn = builtInFlags.find((builtInFlag) => builtInFlag.name === flag);
984-
985-
if (inBuiltIn) {
986-
return {
987-
...meta,
988-
// @ts-expect-error this might be overwritten
989-
name: flag,
990-
group: "core",
991-
...inBuiltIn,
992-
configs: meta.configs || [],
993-
};
994-
}
995-
996-
return { ...meta, name: flag, group: "core" };
997-
});
998-
999-
const options: WebpackCLIBuiltInOption[] = ([] as WebpackCLIBuiltInFlag[])
1000-
.concat(
1001-
builtInFlags.filter(
1002-
(builtInFlag) =>
1003-
!coreArguments.find((coreArgument) => builtInFlag.name === coreArgument.name),
1004-
),
1005-
)
1006-
.concat(coreArguments)
1007-
.map((option): WebpackCLIBuiltInOption => {
1008-
(option as WebpackCLIBuiltInOption).helpLevel = minimumHelpFlags.includes(option.name)
1009-
? "minimum"
1010-
: "verbose";
1011-
return option as WebpackCLIBuiltInOption;
1012-
});
994+
const options = builtInFlags.concat(
995+
Object.entries(this.webpack.cli.getArguments()).map<WebpackCLIBuiltInOption>(
996+
([name, meta]) => {
997+
return {
998+
...meta,
999+
name,
1000+
group: "core",
1001+
helpLevel: minimumHelpFlags.includes(name) ? "minimum" : "verbose",
1002+
};
1003+
},
1004+
),
1005+
);
10131006

10141007
this.builtInOptionsCache = options;
10151008

test/build/json/logging.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
mode: "development",
23
infrastructureLogging: {
34
level: "log",
45
},

0 commit comments

Comments
 (0)