Skip to content

Commit

Permalink
refactor: reduce loops (#3744)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Apr 20, 2023
1 parent fb3671a commit 3b0f74f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
4 changes: 4 additions & 0 deletions packages/generators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GeneratorsCommand {
configs: [{ type: "string" }],
description: "Type of template",
defaultValue: "default",
helpLevel: "minimum",
},
{
name: "force",
Expand All @@ -37,6 +38,7 @@ class GeneratorsCommand {
},
],
description: "Generate without questions (ideally) using default answers",
helpLevel: "minimum",
},
],
async (generationPath: string, options: InitOptions) => {
Expand Down Expand Up @@ -76,6 +78,7 @@ class GeneratorsCommand {
configs: [{ type: "string" }],
description: "Type of template",
defaultValue: "default",
helpLevel: "minimum",
},
],
async (outputPath: string, options: LoaderOptions) => {
Expand Down Expand Up @@ -115,6 +118,7 @@ class GeneratorsCommand {
configs: [{ type: "string" }],
description: "Type of template",
defaultValue: "default",
helpLevel: "minimum",
},
],
async (outputPath: string, options: PluginOptions) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ interface WebpackCLIBuiltInFlag {
describe?: string;
negatedDescription?: string;
defaultValue?: string;
helpLevel: "minimum" | "verbose";
}

interface WebpackCLIBuiltInOption extends WebpackCLIBuiltInFlag {
hidden?: boolean;
group?: "core";
helpLevel?: "minimum" | "verbose";
}

type WebpackCLIExternalCommandInfo = Pick<WebpackCLIOptions, "name" | "alias" | "description"> & {
Expand Down
57 changes: 25 additions & 32 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,15 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "To get the output in a specified format ( accept json or markdown )",
helpLevel: "minimum",
},
{
name: "additional-package",
alias: "a",
configs: [{ type: "string" }],
multiple: true,
description: "Adds additional packages to the output",
helpLevel: "minimum",
},
];
}
Expand Down Expand Up @@ -810,6 +812,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: true,
description: "Provide path to a webpack configuration file e.g. ./webpack.config.js.",
helpLevel: "minimum",
},
{
name: "config-name",
Expand All @@ -820,6 +823,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: true,
description: "Name of the configuration to use.",
helpLevel: "minimum",
},
{
name: "merge",
Expand All @@ -831,6 +835,7 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Merge two or more configurations using 'webpack-merge'.",
helpLevel: "minimum",
},
{
name: "disable-interpret",
Expand All @@ -841,6 +846,7 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Disable interpret for loading the config file.",
helpLevel: "minimum",
},
// Complex configs
{
Expand Down Expand Up @@ -888,6 +894,7 @@ class WebpackCLI implements IWebpackCLI {
},
multiple: true,
description: "Environment passed to the configuration when it is a function.",
helpLevel: "minimum",
},
{
name: "node-env",
Expand All @@ -898,6 +905,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: false,
description: "Sets process.env.NODE_ENV to the specified value.",
helpLevel: "minimum",
},
{
name: "define-process-env-node-env",
Expand All @@ -909,6 +917,7 @@ class WebpackCLI implements IWebpackCLI {
multiple: false,
description:
"Sets process.env.NODE_ENV to the specified value. (Currently an alias for `--node-env`)",
helpLevel: "minimum",
},

// Adding more plugins
Expand All @@ -922,6 +931,7 @@ class WebpackCLI implements IWebpackCLI {
],
multiple: false,
description: "It invokes webpack-bundle-analyzer plugin to get bundle information.",
helpLevel: "minimum",
},
{
name: "progress",
Expand All @@ -935,6 +945,7 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Print compilation progress during build.",
helpLevel: "minimum",
},

// Output options
Expand All @@ -951,6 +962,7 @@ class WebpackCLI implements IWebpackCLI {
],
alias: "j",
description: "Prints result as JSON or store it in a file.",
helpLevel: "minimum",
},
{
name: "fail-on-warnings",
Expand All @@ -961,11 +973,11 @@ class WebpackCLI implements IWebpackCLI {
},
],
description: "Stop webpack-cli process with non-zero exit code on warnings from webpack",
helpLevel: "minimum",
},
];

const minimumHelpFlags = [
...builtInFlags.map((flag) => flag.name),
"mode",
"watch",
"watch-options-stdin",
Expand All @@ -979,37 +991,18 @@ class WebpackCLI implements IWebpackCLI {

// Extract all the flags being exported from core.
// 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
const coreArguments = Object.entries(this.webpack.cli.getArguments()).map(([flag, meta]) => {
const inBuiltIn = builtInFlags.find((builtInFlag) => builtInFlag.name === flag);

if (inBuiltIn) {
return {
...meta,
// @ts-expect-error this might be overwritten
name: flag,
group: "core",
...inBuiltIn,
configs: meta.configs || [],
};
}

return { ...meta, name: flag, group: "core" };
});

const options: WebpackCLIBuiltInOption[] = ([] as WebpackCLIBuiltInFlag[])
.concat(
builtInFlags.filter(
(builtInFlag) =>
!coreArguments.find((coreArgument) => builtInFlag.name === coreArgument.name),
),
)
.concat(coreArguments)
.map((option): WebpackCLIBuiltInOption => {
(option as WebpackCLIBuiltInOption).helpLevel = minimumHelpFlags.includes(option.name)
? "minimum"
: "verbose";
return option as WebpackCLIBuiltInOption;
});
const options = builtInFlags.concat(
Object.entries(this.webpack.cli.getArguments()).map<WebpackCLIBuiltInOption>(
([name, meta]) => {
return {
...meta,
name,
group: "core",
helpLevel: minimumHelpFlags.includes(name) ? "minimum" : "verbose",
};
},
),
);

this.builtInOptionsCache = options;

Expand Down
1 change: 1 addition & 0 deletions test/build/json/logging.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
mode: "development",
infrastructureLogging: {
level: "log",
},
Expand Down

0 comments on commit 3b0f74f

Please sign in to comment.