From 638e96a87fed843d1b2f6cf8d029836240902474 Mon Sep 17 00:00:00 2001 From: kingdaro Date: Fri, 15 Dec 2017 10:06:15 -0500 Subject: [PATCH 1/2] cleanup: refactor commit minus init-generator --- bin/convert-argv.js | 6 +- lib/generators/add-generator.js | 132 ++++++++++--------- lib/generators/remove-generator.js | 10 +- lib/generators/update-generator.js | 10 +- lib/init/transformations/index.js | 5 +- lib/migrate/uglifyJsPlugin/uglifyJsPlugin.js | 28 ++-- 6 files changed, 100 insertions(+), 91 deletions(-) diff --git a/bin/convert-argv.js b/bin/convert-argv.js index 70866ebd965..037e3f20eac 100644 --- a/bin/convert-argv.js +++ b/bin/convert-argv.js @@ -609,9 +609,9 @@ module.exports = function(yargs, argv, convertOptions) { if (fs.existsSync(resolved)) { addTo( "main", - `${resolved}${fs.statSync(resolved).isDirectory() - ? path.sep - : ""}` + `${resolved}${ + fs.statSync(resolved).isDirectory() ? path.sep : "" + }` ); } else { addTo("main", content); diff --git a/lib/generators/add-generator.js b/lib/generators/add-generator.js index 77d9f750325..7db5534675c 100644 --- a/lib/generators/add-generator.js +++ b/lib/generators/add-generator.js @@ -27,6 +27,7 @@ module.exports = class AddGenerator extends Generator { prompting() { let done = this.async(); let action; + this.prompt([ List( "actionType", @@ -38,74 +39,77 @@ module.exports = class AddGenerator extends Generator { this.configuration.config.webpackOptions[ actionTypeAnswer.actionType ] = null; + action = actionTypeAnswer.actionType; - }) - .then(() => { - this.prompt([ - Input("actionAnswer", `what do you want to add to ${action}?`) - ]).then(answerToAction => { - if (action === ("plugins" || "loader")) { - // - const pluginExist = glob - .sync([ - "node_modules/webpack/lib/*Plugin.js", - "node_modules/webpack/lib/**/*Plugin.js" - ]) - .map(p => - p - .split("/") - .pop() - .replace(".js", "") - ) - .find( - p => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0 - ); - if (pluginExist) { - this.configuration.config.item = pluginExist; - this.configuration.config.webpackOptions[ - action - ] = `new webpack.${pluginExist}`; - done(); - } else { - npmExists(answerToAction.actionAnswer).then(p => { - if (p) { - this.dependencies.push(answerToAction.actionAnswer); - const normalizePluginName = answerToAction.actionAnswer.replace( - "-webpack-plugin", - "Plugin" - ); - const pluginName = replaceAt( - normalizePluginName, - 0, - normalizePluginName.charAt(0).toUpperCase() - ); - this.configuration.config.topScope.push( - `const ${pluginName} = require("${answerToAction.actionAnswer}")` - ); - this.configuration.config.webpackOptions[ - action - ] = `new ${pluginName}`; - this.configuration.config.item = answerToAction.actionAnswer; - done(); - this.runInstall(getPackageManager(), this.dependencies, { - "save-dev": true - }); - } else { - console.error( - answerToAction.actionAnswer, - "doesn't exist on NPM or is built in webpack, please check for any misspellings." - ); - process.exit(0); - } - }); - } - } else { - this.configuration.config.item = action; - this.configuration.config.webpackOptions[action] = "hello"; + return this.prompt([ + Input("actionAnswer", `what do you want to add to ${action}?`) + ]); + }) + .then(answerToAction => { + if (action === "plugins" || action === "loader") { + // + const pluginExist = glob + .sync([ + "node_modules/webpack/lib/*Plugin.js", + "node_modules/webpack/lib/**/*Plugin.js" + ]) + .map(p => + p + .split("/") + .pop() + .replace(".js", "") + ) + .find( + p => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0 + ); + if (pluginExist) { + this.configuration.config.item = pluginExist; + this.configuration.config.webpackOptions[ + action + ] = `new webpack.${pluginExist}`; done(); + } else { + npmExists(answerToAction.actionAnswer).then(p => { + if (p) { + this.dependencies.push(answerToAction.actionAnswer); + const normalizePluginName = answerToAction.actionAnswer.replace( + "-webpack-plugin", + "Plugin" + ); + const pluginName = replaceAt( + normalizePluginName, + 0, + normalizePluginName.charAt(0).toUpperCase() + ); + this.configuration.config.topScope.push( + `const ${pluginName} = require("${ + answerToAction.actionAnswer + }")` + ); + this.configuration.config.webpackOptions[ + action + ] = `new ${pluginName}`; + this.configuration.config.item = answerToAction.actionAnswer; + done(); + + this.runInstall(getPackageManager(), this.dependencies, { + "save-dev": true + }); + } else { + console.error( + answerToAction.actionAnswer, + "doesn't exist on NPM or is built in webpack, please check for any misspellings." + ); + process.exit(0); + } + }); } - }); + } else { + this.configuration.config.item = action; + this.configuration.config.webpackOptions[action] = "hello"; + done(); + } }); } }; diff --git a/lib/generators/remove-generator.js b/lib/generators/remove-generator.js index 116134f0263..ddbcfe73553 100644 --- a/lib/generators/remove-generator.js +++ b/lib/generators/remove-generator.js @@ -17,6 +17,7 @@ module.exports = class RemoveGenerator extends Generator { prompting() { let done = this.async(); let action; + this.prompt([ RawList( "actionType", @@ -32,15 +33,16 @@ module.exports = class RemoveGenerator extends Generator { action = actionTypeAnswer.actionType; }) .then(() => { - this.prompt([ + return this.prompt([ RawList( "actionAnswer", `what do you want to remove on ${action}?` /*types*/ ) - ]).then(answerToAction => { - console.log(answerToAction); - }); + ]); + }) + .then(answerToAction => { + console.log(answerToAction); }); } }; diff --git a/lib/generators/update-generator.js b/lib/generators/update-generator.js index 074f5011a80..745769f7405 100644 --- a/lib/generators/update-generator.js +++ b/lib/generators/update-generator.js @@ -17,6 +17,7 @@ module.exports = class UpdateGenerator extends Generator { prompting() { let done = this.async(); let action; + this.prompt([ RawList( "actionType", @@ -32,15 +33,16 @@ module.exports = class UpdateGenerator extends Generator { action = actionTypeAnswer.actionType; }) .then(() => { - this.prompt([ + return this.prompt([ RawList( "actionAnswer", `what do you want to update to ${action}?` /*types*/ ) - ]).then(answerToAction => { - console.log(answerToAction); - }); + ]); + }) + .then(answerToAction => { + console.log(answerToAction); }); } }; diff --git a/lib/init/transformations/index.js b/lib/init/transformations/index.js index 1f157754a4d..457489b7c83 100644 --- a/lib/init/transformations/index.js +++ b/lib/init/transformations/index.js @@ -122,8 +122,9 @@ module.exports = function runTransform(webpackProperties, action) { process.stdout.write( "\n" + chalk.green( - `Congratulations! ${webpackProperties.config - .item} has been ${action}ed!\n` + `Congratulations! ${ + webpackProperties.config.item + } has been ${action}ed!\n` ) ); } else { diff --git a/lib/migrate/uglifyJsPlugin/uglifyJsPlugin.js b/lib/migrate/uglifyJsPlugin/uglifyJsPlugin.js index a0654b0e556..f0c8e0d46f9 100644 --- a/lib/migrate/uglifyJsPlugin/uglifyJsPlugin.js +++ b/lib/migrate/uglifyJsPlugin/uglifyJsPlugin.js @@ -9,20 +9,20 @@ module.exports = function(j: Ijscodeshit, ast: IPath<*>) { return j.property("init", j.identifier("sourceMap"), j.identifier("true")); } - return findPluginsByName(j, ast, [ - "webpack.optimize.UglifyJsPlugin" - ]).forEach(path => { - const args = path.value.arguments; + return findPluginsByName(j, ast, ["webpack.optimize.UglifyJsPlugin"]).forEach( + path => { + const args = path.value.arguments; - if (args.length) { - // Plugin is called with object as arguments - j(path) - .find(j.ObjectExpression) - .get("properties") - .value.push(createSourceMapsProperty()); - } else { - // Plugin is called without arguments - args.push(j.objectExpression([createSourceMapsProperty()])); + if (args.length) { + // Plugin is called with object as arguments + j(path) + .find(j.ObjectExpression) + .get("properties") + .value.push(createSourceMapsProperty()); + } else { + // Plugin is called without arguments + args.push(j.objectExpression([createSourceMapsProperty()])); + } } - }); + ); }; From 2e763387d7d88102f90860bfeb5bd1c8ecb947ec Mon Sep 17 00:00:00 2001 From: kingdaro Date: Fri, 15 Dec 2017 10:47:08 -0500 Subject: [PATCH 2/2] cleanup: oops, forgot some spots --- lib/generators/add-generator.js | 155 ++++++++++++++++---------------- 1 file changed, 78 insertions(+), 77 deletions(-) diff --git a/lib/generators/add-generator.js b/lib/generators/add-generator.js index 9195fff53a7..48b58922587 100644 --- a/lib/generators/add-generator.js +++ b/lib/generators/add-generator.js @@ -83,87 +83,88 @@ module.exports = class AddGenerator extends Generator { `what do you want to add to ${action}?` ); } - this.prompt([manualOrListInput]).then(answerToAction => { - if (action === ("plugins" || "loader")) { - const pluginExist = glob - .sync([ - "node_modules/webpack/lib/*Plugin.js", - "node_modules/webpack/lib/**/*Plugin.js" - ]) - .map(p => - p - .split("/") - .pop() - .replace(".js", "") - ) - .find( - p => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0 - ); - if (pluginExist) { - this.configuration.config.item = pluginExist; - this.configuration.config.webpackOptions[ - action - ] = `new webpack.${pluginExist}`; - done(); - } else { - npmExists(answerToAction.actionAnswer).then(p => { - if (p) { - this.dependencies.push(answerToAction.actionAnswer); - const normalizePluginName = answerToAction.actionAnswer.replace( - "-webpack-plugin", - "Plugin" - ); - const pluginName = replaceAt( - normalizePluginName, - 0, - normalizePluginName.charAt(0).toUpperCase() - ); - this.configuration.config.topScope.push( - `const ${pluginName} = require("${ - answerToAction.actionAnswer - }")` - ); - this.configuration.config.webpackOptions[ - action - ] = `new ${pluginName}`; - this.configuration.config.item = answerToAction.actionAnswer; - done(); - - this.runInstall(getPackageManager(), this.dependencies, { - "save-dev": true - }); - } else { - console.error( - answerToAction.actionAnswer, - "doesn't exist on NPM or is built in webpack, please check for any misspellings." - ); - process.exit(0); - } - }); - } + return this.prompt([manualOrListInput]); + }) + .then(answerToAction => { + if (action === "plugins" || action === "loader") { + const pluginExist = glob + .sync([ + "node_modules/webpack/lib/*Plugin.js", + "node_modules/webpack/lib/**/*Plugin.js" + ]) + .map(p => + p + .split("/") + .pop() + .replace(".js", "") + ) + .find( + p => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0 + ); + if (pluginExist) { + this.configuration.config.item = pluginExist; + this.configuration.config.webpackOptions[ + action + ] = `new webpack.${pluginExist}`; + done(); } else { - if (isDeepProp[0]) { - isDeepProp[1] = answerToAction.actionAnswer; - this.prompt([ - Input( - "deepProp", - `what do you want the value of ${isDeepProp[1]} to be?` - ) - ]).then(deepPropAns => { - this.configuration.config.item = action + "." + isDeepProp[1]; - this.configuration.config.webpackOptions[action] = { - [isDeepProp[1]]: `'${deepPropAns.deepProp}'` - }; + npmExists(answerToAction.actionAnswer).then(p => { + if (p) { + this.dependencies.push(answerToAction.actionAnswer); + const normalizePluginName = answerToAction.actionAnswer.replace( + "-webpack-plugin", + "Plugin" + ); + const pluginName = replaceAt( + normalizePluginName, + 0, + normalizePluginName.charAt(0).toUpperCase() + ); + this.configuration.config.topScope.push( + `const ${pluginName} = require("${ + answerToAction.actionAnswer + }")` + ); + this.configuration.config.webpackOptions[ + action + ] = `new ${pluginName}`; + this.configuration.config.item = answerToAction.actionAnswer; done(); - }); - } else { - this.configuration.config.item = action; - this.configuration.config.webpackOptions[action] = - answerToAction.actionAnswer; + + this.runInstall(getPackageManager(), this.dependencies, { + "save-dev": true + }); + } else { + console.error( + answerToAction.actionAnswer, + "doesn't exist on NPM or is built in webpack, please check for any misspellings." + ); + process.exit(0); + } + }); + } + } else { + if (isDeepProp[0]) { + isDeepProp[1] = answerToAction.actionAnswer; + this.prompt([ + Input( + "deepProp", + `what do you want the value of ${isDeepProp[1]} to be?` + ) + ]).then(deepPropAns => { + this.configuration.config.item = action + "." + isDeepProp[1]; + this.configuration.config.webpackOptions[action] = { + [isDeepProp[1]]: `'${deepPropAns.deepProp}'` + }; done(); - } + }); + } else { + this.configuration.config.item = action; + this.configuration.config.webpackOptions[action] = + answerToAction.actionAnswer; + done(); } - }); + } }); } };