Skip to content

Commit

Permalink
Refactor bin directory (#263)
Browse files Browse the repository at this point in the history
* Refactor bin directory

* Convert missed vars to consts
  • Loading branch information
EugeneHlushko authored and evenstensberg committed Feb 12, 2018
1 parent 73fdf7d commit 1e846cb
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 58 deletions.
16 changes: 8 additions & 8 deletions bin/config-yargs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var CONFIG_GROUP = "Config options:";
var BASIC_GROUP = "Basic options:";
var MODULE_GROUP = "Module options:";
var OUTPUT_GROUP = "Output options:";
var ADVANCED_GROUP = "Advanced options:";
var RESOLVE_GROUP = "Resolving options:";
var OPTIMIZE_GROUP = "Optimizing options:";
var INIT_GROUP = "Initialization:";
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 INIT_GROUP = "Initialization:";

module.exports = function(yargs) {
yargs
Expand Down
34 changes: 17 additions & 17 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var path = require("path");
var fs = require("fs");
const path = require("path");
const fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
var interpret = require("interpret");
var prepareOptions = require("./prepareOptions");
var webpackConfigurationSchema = require("../schemas/webpackConfigurationSchema.json");
var validateSchema = require("webpack").validateSchema;
var WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError;
const interpret = require("interpret");
const prepareOptions = require("./prepareOptions");
const webpackConfigurationSchema = require("../schemas/webpackConfigurationSchema.json");
const validateSchema = require("webpack").validateSchema;
const WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError;

module.exports = function(...args) {
var argv = args[1] || args[0];
var options = [];
const argv = args[1] || args[0];
const options = [];
// Shortcuts
if (argv.d) {
argv.debug = true;
Expand Down Expand Up @@ -58,11 +58,11 @@ module.exports = function(...args) {
return a.concat(i);
}, []);

var i;
let i;
if (argv.config) {
var getConfigExtension = function getConfigExtension(configPath) {
const getConfigExtension = function getConfigExtension(configPath) {
for (i = extensions.length - 1; i >= 0; i--) {
var tmpExt = extensions[i];
const tmpExt = extensions[i];
if (
configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1
) {
Expand All @@ -72,22 +72,22 @@ module.exports = function(...args) {
return path.extname(configPath);
};

var mapConfigArg = function mapConfigArg(configArg) {
var resolvedPath = path.resolve(configArg);
var extension = getConfigExtension(resolvedPath);
const mapConfigArg = function mapConfigArg(configArg) {
const resolvedPath = path.resolve(configArg);
const extension = getConfigExtension(resolvedPath);
return {
path: resolvedPath,
ext: extension
};
};

var configArgList = Array.isArray(argv.config)
const configArgList = Array.isArray(argv.config)
? argv.config
: [argv.config];
configFiles = configArgList.map(mapConfigArg);
} else {
for (i = 0; i < defaultConfigFiles.length; i++) {
var webpackConfig = defaultConfigFiles[i].path;
const webpackConfig = defaultConfigFiles[i].path;
if (fs.existsSync(webpackConfig)) {
configFiles.push({
path: webpackConfig,
Expand Down
4 changes: 2 additions & 2 deletions bin/errorHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const webpackOptionsFlag = "WEBPACK_OPTIONS";
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;
if (stack[i].includes(flag)) stack.length = i;
return stack.join("\n");
};

Expand All @@ -28,7 +28,7 @@ exports.cutOffMultilineMessage = (stack, message) => {
return stack
.reduce(
(acc, line, idx) =>
line.indexOf(message[idx]) < 0 ? acc.concat(line) : acc,
line.includes(message[idx]) ? acc : acc.concat(line),
[]
)
.join("\n");
Expand Down
22 changes: 11 additions & 11 deletions bin/process-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function processOptions(yargs, argv) {
fn(argv[name], -1);
}
}
var options = require("./convert-argv")(argv);
const options = require("./convert-argv")(argv);

if (typeof options.then === "function") {
options.then(processOptions).catch(function(err) {
Expand All @@ -19,14 +19,14 @@ module.exports = function processOptions(yargs, argv) {
return;
}

var firstOptions = Array.isArray(options) ? options[0] || {} : options;
const firstOptions = Array.isArray(options) ? options[0] || {} : options;

if (typeof options.stats === "boolean" || typeof options.stats === "string") {
var statsPresetToOptions = require("webpack/lib/Stats.js").presetToOptions;
const statsPresetToOptions = require("webpack/lib/Stats.js").presetToOptions;
options.stats = statsPresetToOptions(options.stats);
}

var outputOptions = Object.create(options.stats || firstOptions.stats || {});
const outputOptions = Object.create(options.stats || firstOptions.stats || {});
if (typeof outputOptions.context === "undefined")
outputOptions.context = firstOptions.context;

Expand Down Expand Up @@ -127,15 +127,15 @@ module.exports = function processOptions(yargs, argv) {
}
});

var webpack = require("webpack/lib/webpack.js");
const webpack = require("webpack/lib/webpack.js");

Error.stackTraceLimit = 30;
var lastHash = null;
var compiler;
let lastHash = null;
let compiler;
try {
compiler = webpack(options);
} catch (e) {
var WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
const WebpackOptionsValidationError = require("webpack/lib/WebpackOptionsValidationError");
if (e instanceof WebpackOptionsValidationError) {
if (argv.color)
console.error(
Expand All @@ -148,7 +148,7 @@ module.exports = function processOptions(yargs, argv) {
}

if (argv.progress) {
var ProgressPlugin = require("webpack/lib/ProgressPlugin");
const ProgressPlugin = require("webpack/lib/ProgressPlugin");
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down Expand Up @@ -184,8 +184,8 @@ module.exports = function processOptions(yargs, argv) {
}
}
if (options.watch) {
var primaryOptions = !Array.isArray(options) ? options : options[0];
var watchOptions =
const primaryOptions = !Array.isArray(options) ? options : options[0];
const watchOptions =
primaryOptions.watchOptions || primaryOptions.watch || {};
if (watchOptions.stdin) {
process.stdin.on("end", function(_) {
Expand Down
42 changes: 22 additions & 20 deletions bin/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
(function() {
// wrap in IIFE to be able to use return

var resolveCwd = require("resolve-cwd");
const resolveCwd = require("resolve-cwd");
// Local version replace global one
var localCLI = resolveCwd.silent("webpack-cli/bin/webpack");
const localCLI = resolveCwd.silent("webpack-cli/bin/webpack");
if (localCLI && localCLI !== __filename) {
require(localCLI);
return;
}

require("v8-compile-cache");
var ErrorHelpers = require("./errorHelpers");
const ErrorHelpers = require("./errorHelpers");

const NON_COMPILATION_ARGS = [
"init",
Expand Down Expand Up @@ -47,7 +47,7 @@
return;
}

var yargs = require("yargs").usage(
const yargs = require("yargs").usage(
"webpack-cli " +
require("../package.json").version +
"\n" +
Expand All @@ -58,8 +58,8 @@

require("./config-yargs")(yargs);

var DISPLAY_GROUP = "Stats options:";
var BASIC_GROUP = "Basic options:";
const DISPLAY_GROUP = "Stats options:";
const BASIC_GROUP = "Basic options:";

yargs.options({
silent: {
Expand Down Expand Up @@ -220,15 +220,16 @@
argv["display"] = "verbose";
}

let options;
try {
var options = require("./convert-argv")(argv);
options = require("./convert-argv")(argv);
} catch (err) {
if (err.name !== "ValidationError") {
throw err;
}

var stack = ErrorHelpers.cleanUpWebpackOptions(err.stack, err.message);
var message = err.message + "\n" + stack;
const stack = ErrorHelpers.cleanUpWebpackOptions(err.stack, err.message);
const message = err.message + "\n" + stack;

if (argv.color) {
console.error(`\u001b[1m\u001b[31m${message}\u001b[39m\u001b[22m`);
Expand All @@ -244,7 +245,7 @@
* When --silent flag is present, an object with a no-op write method is
* used in place of process.stout
*/
var stdout = argv.silent
const stdout = argv.silent
? {
write: () => {}
}
Expand All @@ -270,11 +271,11 @@
return;
}

var firstOptions = [].concat(options)[0];
var statsPresetToOptions = require("webpack/lib/Stats.js")
const firstOptions = [].concat(options)[0];
const statsPresetToOptions = require("webpack/lib/Stats.js")
.presetToOptions;

var outputOptions = options.stats;
let outputOptions = options.stats;
if (
typeof outputOptions === "boolean" ||
typeof outputOptions === "string"
Expand Down Expand Up @@ -414,10 +415,10 @@
outputOptions.infoVerbosity = value;
});

var webpack = require("webpack/lib/webpack.js");
const webpack = require("webpack/lib/webpack.js");

var lastHash = null;
var compiler;
let lastHash = null;
let compiler;
try {
compiler = webpack(options);
} catch (err) {
Expand All @@ -426,7 +427,8 @@
console.error(
`\u001b[1m\u001b[31m${err.message}\u001b[39m\u001b[22m`
);
else console.error(err.message);
else
console.error(err.message);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
Expand All @@ -435,7 +437,7 @@
}

if (argv.progress) {
var ProgressPlugin = require("webpack/lib/ProgressPlugin");
const ProgressPlugin = require("webpack/lib/ProgressPlugin");
compiler.apply(
new ProgressPlugin({
profile: argv.profile
Expand Down Expand Up @@ -469,15 +471,15 @@
);
} else if (stats.hash !== lastHash) {
lastHash = stats.hash;
var statsString = stats.toString(outputOptions);
const statsString = stats.toString(outputOptions);
if (statsString) stdout.write(statsString + "\n");
}
if (!options.watch && stats.hasErrors()) {
process.exitCode = 2;
}
}
if (firstOptions.watch || options.watch) {
var watchOptions =
const watchOptions =
firstOptions.watchOptions ||
firstOptions.watch ||
options.watch ||
Expand Down

0 comments on commit 1e846cb

Please sign in to comment.