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

chore(cli): move constants to a separate file #798

Merged
merged 6 commits into from Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 3 additions & 13 deletions bin/cli.js
Expand Up @@ -5,6 +5,8 @@
Author Tobias Koppers @sokra
*/

const { NON_COMPILATION_ARGS } = require("./utils/constants");

(function() {
// wrap in IIFE to be able to use return

Expand All @@ -18,17 +20,6 @@

const ErrorHelpers = require("./utils/errorHelpers");

const NON_COMPILATION_ARGS = [
"init",
"migrate",
"add",
"remove",
"serve",
"generate-loader",
"generate-plugin",
"info"
];

const NON_COMPILATION_CMD = process.argv.find(arg => {
if (arg === "serve") {
global.process.argv = global.process.argv.filter(a => a !== "serve");
Expand All @@ -52,7 +43,6 @@ For more information, see https://webpack.js.org/api/cli/.`);

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).
// To prevent this we use the yargs.parse API and exit the process normally
Expand Down Expand Up @@ -104,7 +94,7 @@ For more information, see https://webpack.js.org/api/cli/.`);
const stdout = argv.silent
? {
write: () => {}
} // eslint-disable-line
} // eslint-disable-line
: process.stdout;

function ifArg(name, fn, init) {
Expand Down
22 changes: 13 additions & 9 deletions bin/config/config-yargs.js
@@ -1,5 +1,18 @@
const optionsSchema = require("../config/optionsSchema.json");

const { GROUPS } = require("../utils/constants");

const {
CONFIG_GROUP,
BASIC_GROUP,
MODULE_GROUP,
OUTPUT_GROUP,
ADVANCED_GROUP,
RESOLVE_GROUP,
OPTIMIZE_GROUP,
DISPLAY_GROUP
} = GROUPS;

const nestedProperties = ["anyOf", "oneOf", "allOf"];

const resolveSchema = schema => {
Expand Down Expand Up @@ -52,15 +65,6 @@ const getSchemaInfo = (path, property, subProperty) => {
return findPropertyInSchema(current, property, subProperty);
};

const CONFIG_GROUP = "Config options:";
const BASIC_GROUP = "Basic options:";
const MODULE_GROUP = "Module options:";
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
.help("help")
Expand Down
37 changes: 37 additions & 0 deletions bin/utils/constants.js
@@ -0,0 +1,37 @@
const NON_COMPILATION_ARGS = [
"init",
"migrate",
"add",
"remove",
"serve",
"generate-loader",
"generate-plugin",
"info"
];

const CONFIG_GROUP = "Config options:";
const BASIC_GROUP = "Basic options:";
const MODULE_GROUP = "Module options:";
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:";
const GROUPS = {
CONFIG_GROUP,
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't be more compact to initialize the constants inside the JSON? :)

const GROUPS = {
	CONFIG_GROUP: "Config options:"

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you mean by "compact"?

BASIC_GROUP,
MODULE_GROUP,
OUTPUT_GROUP,
ADVANCED_GROUP,
RESOLVE_GROUP,
OPTIMIZE_GROUP,
DISPLAY_GROUP
};

const WEBPACK_OPTIONS_FLAG = "WEBPACK_OPTIONS";

module.exports = {
NON_COMPILATION_ARGS,
GROUPS,
WEBPACK_OPTIONS_FLAG
};
4 changes: 2 additions & 2 deletions bin/utils/errorHelpers.js
Expand Up @@ -4,15 +4,15 @@
*/
"use strict";

const webpackOptionsFlag = "WEBPACK_OPTIONS";
const { WEBPACK_OPTIONS_FLAG } = require("./constants");

exports.cutOffByFlag = (stack, flag) => {
stack = stack.split("\n");
for (let i = 0; i < stack.length; i++) if (stack[i].indexOf(flag) >= 0) stack.length = i;
return stack.join("\n");
};

exports.cutOffWebpackOptions = stack => exports.cutOffByFlag(stack, webpackOptionsFlag);
exports.cutOffWebpackOptions = stack => exports.cutOffByFlag(stack, WEBPACK_OPTIONS_FLAG);

exports.cutOffMultilineMessage = (stack, message) => {
stack = stack.split("\n");
Expand Down