From 9c46fb68e22e609e748e9d444cdb0a00fbbc562a Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Sat, 9 Mar 2019 11:57:31 +0530 Subject: [PATCH] misc(cli): refactor functions into utils and config dirs, merge yargs options Moved filed to respective config or utils dir, cleaned up cli.js and removed yargs options and merged in a single file ISSUES CLOSED: #770 --- bin/cli.js | 157 +----------------- bin/{ => config}/config-yargs.js | 145 +++++++++++++++- bin/{ => config}/optionsSchema.json | 0 .../webpackConfigurationSchema.json | 0 bin/{ => utils}/convert-argv.js | 2 +- bin/{ => utils}/errorHelpers.js | 0 bin/{ => utils}/prepareOptions.js | 0 bin/{ => utils}/prompt-command.js | 0 8 files changed, 150 insertions(+), 154 deletions(-) rename bin/{ => config}/config-yargs.js (70%) rename bin/{ => config}/optionsSchema.json (100%) rename bin/{ => config}/webpackConfigurationSchema.json (100%) rename bin/{ => utils}/convert-argv.js (99%) rename bin/{ => utils}/errorHelpers.js (100%) rename bin/{ => utils}/prepareOptions.js (100%) rename bin/{ => utils}/prompt-command.js (100%) diff --git a/bin/cli.js b/bin/cli.js index b0dc2649d2d..99e3a92c0ec 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -16,7 +16,7 @@ require("v8-compile-cache"); - const ErrorHelpers = require("./errorHelpers"); + const ErrorHelpers = require("./utils/errorHelpers"); const NON_COMPILATION_ARGS = [ "init", @@ -38,7 +38,7 @@ }); if (NON_COMPILATION_CMD) { - return require("./prompt-command")(NON_COMPILATION_CMD, ...process.argv); + return require("./utils/prompt-command")(NON_COMPILATION_CMD, ...process.argv); } const yargs = require("yargs").usage(`webpack-cli ${require("../package.json").version} @@ -50,155 +50,8 @@ Usage: webpack-cli [options] For more information, see https://webpack.js.org/api/cli/.`); - require("./config-yargs")(yargs); - - const DISPLAY_GROUP = "Stats options:"; - const BASIC_GROUP = "Basic options:"; - - yargs.options({ - silent: { - type: "boolean", - describe: "Prevent output from being displayed in stdout" - }, - json: { - type: "boolean", - alias: "j", - describe: "Prints the result as JSON." - }, - progress: { - type: "boolean", - describe: "Print compilation progress in percentage", - group: BASIC_GROUP - }, - color: { - type: "boolean", - alias: "colors", - default: function supportsColor() { - return require("supports-color").stdout; - }, - group: DISPLAY_GROUP, - describe: "Force colors on the console" - }, - "no-color": { - type: "boolean", - alias: "no-colors", - group: DISPLAY_GROUP, - describe: "Force no colors on the console" - }, - "sort-modules-by": { - type: "string", - group: DISPLAY_GROUP, - describe: "Sorts the modules list by property in module" - }, - "sort-chunks-by": { - type: "string", - group: DISPLAY_GROUP, - describe: "Sorts the chunks list by property in chunk" - }, - "sort-assets-by": { - type: "string", - group: DISPLAY_GROUP, - describe: "Sorts the assets list by property in asset" - }, - "hide-modules": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Hides info about modules" - }, - "display-exclude": { - type: "string", - group: DISPLAY_GROUP, - describe: "Exclude modules in the output" - }, - "display-modules": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display even excluded modules in the output" - }, - "display-max-modules": { - type: "number", - group: DISPLAY_GROUP, - describe: "Sets the maximum number of visible modules in output" - }, - "display-chunks": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display chunks in the output" - }, - "display-entrypoints": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display entry points in the output" - }, - "display-origins": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display origins of chunks in the output" - }, - "display-cached": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display also cached modules in the output" - }, - "display-cached-assets": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display also cached assets in the output" - }, - "display-reasons": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display reasons about module inclusion in the output" - }, - "display-depth": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display distance from entry point for each module" - }, - "display-used-exports": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display information about used exports in modules (Tree Shaking)" - }, - "display-provided-exports": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display information about exports provided from modules" - }, - "display-optimization-bailout": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display information about why optimization bailed out for modules" - }, - "display-error-details": { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Display details about errors" - }, - display: { - type: "string", - choices: ["", "verbose", "detailed", "normal", "minimal", "errors-only", "none"], - group: DISPLAY_GROUP, - describe: "Select display preset" - }, - verbose: { - type: "boolean", - group: DISPLAY_GROUP, - describe: "Show more details" - }, - "info-verbosity": { - type: "string", - default: "info", - choices: ["none", "info", "verbose"], - group: DISPLAY_GROUP, - describe: "Controls the output of lifecycle messaging e.g. Started watching files..." - }, - "build-delimiter": { - type: "string", - group: DISPLAY_GROUP, - describe: "Display custom text after build output" - } - }); + require("./config/config-yargs")(yargs); + // yargs will terminate the process early when the user uses help or version. // This causes large help outputs to be cut short (https://github.com/nodejs/node/wiki/API-changes-between-v0.10-and-v4#process). @@ -225,7 +78,7 @@ For more information, see https://webpack.js.org/api/cli/.`); let options; try { - options = require("./convert-argv")(argv); + options = require("./utils/convert-argv")(argv); } catch (err) { if (err.name !== "ValidationError") { throw err; diff --git a/bin/config-yargs.js b/bin/config/config-yargs.js similarity index 70% rename from bin/config-yargs.js rename to bin/config/config-yargs.js index 2dc1b8a5805..19a3c489e68 100644 --- a/bin/config-yargs.js +++ b/bin/config/config-yargs.js @@ -1,4 +1,4 @@ -const optionsSchema = require("./optionsSchema.json"); +const optionsSchema = require("../config/optionsSchema.json"); const nestedProperties = ["anyOf", "oneOf", "allOf"]; @@ -59,6 +59,7 @@ const OUTPUT_GROUP = "Output options:"; const ADVANCED_GROUP = "Advanced options:"; const RESOLVE_GROUP = "Resolving options:"; const OPTIMIZE_GROUP = "Optimizing options:"; +const DISPLAY_GROUP = "Stats options:"; module.exports = function(yargs) { yargs @@ -346,6 +347,148 @@ module.exports = function(yargs) { type: "boolean", describe: "shortcut for --optimize-minimize --define process.env.NODE_ENV=\"production\"", group: BASIC_GROUP + }, + silent: { + type: "boolean", + describe: "Prevent output from being displayed in stdout" + }, + json: { + type: "boolean", + alias: "j", + describe: "Prints the result as JSON." + }, + progress: { + type: "boolean", + describe: "Print compilation progress in percentage", + group: BASIC_GROUP + }, + color: { + type: "boolean", + alias: "colors", + default: function supportsColor() { + return require("supports-color").stdout; + }, + group: DISPLAY_GROUP, + describe: "Force colors on the console" + }, + "no-color": { + type: "boolean", + alias: "no-colors", + group: DISPLAY_GROUP, + describe: "Force no colors on the console" + }, + "sort-modules-by": { + type: "string", + group: DISPLAY_GROUP, + describe: "Sorts the modules list by property in module" + }, + "sort-chunks-by": { + type: "string", + group: DISPLAY_GROUP, + describe: "Sorts the chunks list by property in chunk" + }, + "sort-assets-by": { + type: "string", + group: DISPLAY_GROUP, + describe: "Sorts the assets list by property in asset" + }, + "hide-modules": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Hides info about modules" + }, + "display-exclude": { + type: "string", + group: DISPLAY_GROUP, + describe: "Exclude modules in the output" + }, + "display-modules": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display even excluded modules in the output" + }, + "display-max-modules": { + type: "number", + group: DISPLAY_GROUP, + describe: "Sets the maximum number of visible modules in output" + }, + "display-chunks": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display chunks in the output" + }, + "display-entrypoints": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display entry points in the output" + }, + "display-origins": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display origins of chunks in the output" + }, + "display-cached": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display also cached modules in the output" + }, + "display-cached-assets": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display also cached assets in the output" + }, + "display-reasons": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display reasons about module inclusion in the output" + }, + "display-depth": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display distance from entry point for each module" + }, + "display-used-exports": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display information about used exports in modules (Tree Shaking)" + }, + "display-provided-exports": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display information about exports provided from modules" + }, + "display-optimization-bailout": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display information about why optimization bailed out for modules" + }, + "display-error-details": { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Display details about errors" + }, + display: { + type: "string", + choices: ["", "verbose", "detailed", "normal", "minimal", "errors-only", "none"], + group: DISPLAY_GROUP, + describe: "Select display preset" + }, + verbose: { + type: "boolean", + group: DISPLAY_GROUP, + describe: "Show more details" + }, + "info-verbosity": { + type: "string", + default: "info", + choices: ["none", "info", "verbose"], + group: DISPLAY_GROUP, + describe: "Controls the output of lifecycle messaging e.g. Started watching files..." + }, + "build-delimiter": { + type: "string", + group: DISPLAY_GROUP, + describe: "Display custom text after build output" } }); }; diff --git a/bin/optionsSchema.json b/bin/config/optionsSchema.json similarity index 100% rename from bin/optionsSchema.json rename to bin/config/optionsSchema.json diff --git a/bin/webpackConfigurationSchema.json b/bin/config/webpackConfigurationSchema.json similarity index 100% rename from bin/webpackConfigurationSchema.json rename to bin/config/webpackConfigurationSchema.json diff --git a/bin/convert-argv.js b/bin/utils/convert-argv.js similarity index 99% rename from bin/convert-argv.js rename to bin/utils/convert-argv.js index c5fc62f18a8..d1c8d8d2f43 100644 --- a/bin/convert-argv.js +++ b/bin/utils/convert-argv.js @@ -3,7 +3,7 @@ const fs = require("fs"); fs.existsSync = fs.existsSync || path.existsSync; const interpret = require("interpret"); const prepareOptions = require("./prepareOptions"); -const webpackConfigurationSchema = require("./webpackConfigurationSchema.json"); +const webpackConfigurationSchema = require("../config/webpackConfigurationSchema.json"); const validateSchema = require("webpack").validateSchema; const WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError; const findup = require("findup-sync"); diff --git a/bin/errorHelpers.js b/bin/utils/errorHelpers.js similarity index 100% rename from bin/errorHelpers.js rename to bin/utils/errorHelpers.js diff --git a/bin/prepareOptions.js b/bin/utils/prepareOptions.js similarity index 100% rename from bin/prepareOptions.js rename to bin/utils/prepareOptions.js diff --git a/bin/prompt-command.js b/bin/utils/prompt-command.js similarity index 100% rename from bin/prompt-command.js rename to bin/utils/prompt-command.js