Skip to content

Commit f2e62f5

Browse files
cli(refactor): improve folder structure (#371)
* cli(refactor): improve folder structure * chore(linting): fix linter errors * cli(filepath): use local import instead * cli(migrate): refactor error handling * chore(review): fix review comments * chore(review): fix review comments * chore(review): fix review comments
1 parent bdd5b48 commit f2e62f5

36 files changed

+119
-108
lines changed

bin/process-options.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ module.exports = function processOptions(yargs, argv) {
179179
);
180180
} else if (stats.hash !== lastHash) {
181181
lastHash = stats.hash;
182-
const delimiter = outputOptions.buildDelimiter ? `${outputOptions.buildDelimiter}\n` : "";
182+
const delimiter = outputOptions.buildDelimiter
183+
? `${outputOptions.buildDelimiter}\n`
184+
: "";
183185
process.stdout.write("\n" + new Date() + "\n" + "\n");
184186
process.stdout.write(`${stats.toString(outputOptions)}\n${delimiter}`);
185187
if (argv.s) lastHash = null;

bin/webpack.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
(function() {
99
// wrap in IIFE to be able to use return
1010

11-
const resolveCwd = require("resolve-cwd");
12-
// Local version replace global one
13-
const localCLI = resolveCwd.silent("webpack-cli/bin/webpack");
14-
if (localCLI && localCLI !== __filename) {
15-
require(localCLI);
11+
const importLocal = require("import-local");
12+
// Prefer the local installation of webpack-cli
13+
if (importLocal(__filename)) {
1614
return;
1715
}
1816

@@ -48,7 +46,9 @@
4846
return;
4947
}
5048

51-
const yargs = require("yargs").usage(`webpack-cli ${require("../package.json").version}
49+
const yargs = require("yargs").usage(`webpack-cli ${
50+
require("../package.json").version
51+
}
5252
5353
Usage: webpack-cli [options]
5454
webpack-cli [options] --entry <entry> --output <output>
@@ -482,7 +482,9 @@ For more information, see https://webpack.js.org/api/cli/.`);
482482
} else if (stats.hash !== lastHash) {
483483
lastHash = stats.hash;
484484
const statsString = stats.toString(outputOptions);
485-
const delimiter = outputOptions.buildDelimiter ? `${outputOptions.buildDelimiter}\n` : "";
485+
const delimiter = outputOptions.buildDelimiter
486+
? `${outputOptions.buildDelimiter}\n`
487+
: "";
486488
if (statsString) stdout.write(`${statsString}\n${delimiter}`);
487489
}
488490
if (!options.watch && stats.hasErrors()) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { LoaderGenerator } = require("../generators/loader-generator");
55
* Runs a yeoman generator to create a new webpack loader project
66
* @returns {void}
77
*/
8+
89
function loaderCreator() {
910
const env = yeoman.createEnv();
1011
const generatorName = "webpack-loader-generator";
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const yeoman = require("yeoman-environment");
2-
const PluginGenerator = require("../generators/plugin-generator").PluginGenerator;
2+
const { PluginGenerator } = require("../generators/plugin-generator");
33

44
/**
55
* Runs a yeoman generator to create a new webpack plugin project
66
* @returns {void}
77
*/
8+
89
function pluginCreator() {
910
const env = yeoman.createEnv();
1011
const generatorName = "webpack-plugin-generator";

lib/commands/info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function info() {
1313
Binaries: ["Node", "Yarn", "npm"],
1414
Browsers: ["Chrome", "Firefox", "Safari"],
1515
npmPackages: "*webpack*",
16-
npmGlobalPackages: ["webpack", "webpack-cli"],
16+
npmGlobalPackages: ["webpack", "webpack-cli"]
1717
})
1818
);
1919
};

lib/commands/init.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ const modifyHelper = require("../utils/modify-config-helper");
99
* First function to be called after running the init flag. This is a check,
1010
* if we are running the init command with no arguments or if we got dependencies
1111
*
12-
* @param {Object} pkg - packages included when running the init command
12+
* @param {Array} args - array of arguments such as
13+
* packages included when running the init command
1314
* @returns {Function} creator/npmPackagesExists - returns an installation of the package,
1415
* followed up with a yeoman instance of that if there's packages. If not, it creates a defaultGenerator
1516
*/
1617

17-
module.exports = function initializeInquirer(pkg) {
18-
if (pkg.length === 0) {
18+
module.exports = function initializeInquirer(...args) {
19+
const packages = args.slice(3);
20+
21+
if (packages.length === 0) {
1922
return modifyHelper("init", defaultGenerator);
2023
}
21-
return npmPackagesExists(pkg);
24+
return npmPackagesExists(packages);
2225
};

lib/commands/migrate.js

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,83 @@
11
"use strict";
22

33
const fs = require("fs");
4+
const path = require("path");
45
const chalk = require("chalk");
56
const diff = require("diff");
67
const inquirer = require("inquirer");
78
const PLazy = require("p-lazy");
89
const Listr = require("listr");
910

10-
const validate = require("webpack").validate;
11-
const WebpackOptionsValidationError = require("webpack")
12-
.WebpackOptionsValidationError;
11+
const { validate } = require("webpack");
12+
const { WebpackOptionsValidationError } = require("webpack");
1313

1414
const runPrettier = require("../utils/run-prettier");
1515

1616
/**
17-
*
18-
* Runs migration on a given configuration using AST's and promises
19-
* to sequentially transform a configuration file.
20-
*
21-
* @param {String} currentConfigPath - Location of the configuration to be migrated
22-
* @param {String} outputConfigPath - Location to where the configuration should be written
23-
* @param {Object} options - Any additional options regarding code style of the written configuration
17+
*
18+
* Runs migration on a given configuration using AST's and promises
19+
* to sequentially transform a configuration file.
20+
*
21+
* @param {Array} args - Migrate arguments such as input and
22+
* output path
23+
* @returns {Function} Runs the migration using the 'runMigrate'
24+
* function.
25+
*/
2426

25-
* @returns {Promise} Runs the migration using a promise that will throw any errors during each transform
26-
* or output if the user decides to abort the migration
27-
*/
27+
module.exports = function migrate(...args) {
28+
const filePaths = args.slice(3);
29+
if (!filePaths.length) {
30+
const errMsg = "\n ✖ Please specify a path to your webpack config \n ";
31+
console.error(chalk.red(errMsg));
32+
return;
33+
}
34+
const currentConfigPath = path.resolve(process.cwd(), filePaths[0]);
35+
let outputConfigPath;
36+
if (!filePaths[1]) {
37+
return inquirer
38+
.prompt([
39+
{
40+
type: "confirm",
41+
name: "confirmPath",
42+
message:
43+
"Migration output path not specified. " +
44+
"Do you want to use your existing webpack " +
45+
"configuration?",
46+
default: "Y"
47+
}
48+
])
49+
.then(ans => {
50+
if (!ans["confirmPath"]) {
51+
console.error(chalk.red("✖ ︎Migration aborted due no output path"));
52+
return;
53+
}
54+
outputConfigPath = path.resolve(process.cwd(), filePaths[0]);
55+
return runMigration(currentConfigPath, outputConfigPath);
56+
})
57+
.catch(err => {
58+
console.error(err);
59+
});
60+
}
61+
outputConfigPath = path.resolve(process.cwd(), filePaths[1]);
62+
return runMigration(currentConfigPath, outputConfigPath);
63+
};
2864

29-
module.exports = function migrate(
30-
currentConfigPath,
31-
outputConfigPath,
32-
options
33-
) {
34-
const recastOptions = Object.assign(
35-
{
36-
quote: "single"
37-
},
38-
options
39-
);
65+
/**
66+
*
67+
* Runs migration on a given configuration using AST's and promises
68+
* to sequentially transform a configuration file.
69+
*
70+
* @param {String} currentConfigPath - input path for config
71+
* @param {String} outputConfigPath - output path for config
72+
* @returns {Promise} Runs the migration using a promise that
73+
* will throw any errors during each transform or output if the
74+
* user decides to abort the migration
75+
*/
76+
77+
function runMigration(currentConfigPath, outputConfigPath) {
78+
const recastOptions = {
79+
quote: "single"
80+
};
4081
const tasks = new Listr([
4182
{
4283
title: "Reading webpack config",
@@ -145,8 +186,9 @@ module.exports = function migrate(
145186
});
146187
})
147188
.catch(err => {
148-
console.log(chalk.red("✖ ︎Migration aborted due to some errors"));
189+
const errMsg = "\n ✖ ︎Migration aborted due to some errors: \n";
190+
console.error(chalk.red(errMsg));
149191
console.error(err);
150192
process.exitCode = 1;
151193
});
152-
};
194+
}

lib/generate-loader/templates/examples/simple/src/index.js.tpl renamed to lib/generate-loader/examples/simple/src/index.js.tpl

File renamed without changes.

lib/generate-loader/templates/examples/simple/src/lazy-module.js.tpl renamed to lib/generate-loader/examples/simple/src/lazy-module.js.tpl

File renamed without changes.

lib/generate-loader/templates/examples/simple/src/static-esm-module.js.tpl renamed to lib/generate-loader/examples/simple/src/static-esm-module.js.tpl

File renamed without changes.

0 commit comments

Comments
 (0)