Skip to content

Commit

Permalink
Refactor for readability (v2 branch) (#214)
Browse files Browse the repository at this point in the history
* cleanup: refactor commit minus init-generator

* cleanup: oops, forgot some spots
  • Loading branch information
kingdaro authored and evenstensberg committed Dec 15, 2017
1 parent 426e841 commit b639c77
Showing 1 changed file with 79 additions and 77 deletions.
156 changes: 79 additions & 77 deletions lib/generators/add-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = class AddGenerator extends Generator {
this.configuration.config.webpackOptions[
actionTypeAnswer.actionType
] = null;

action = actionTypeAnswer.actionType;
})
.then(() => {
Expand Down Expand Up @@ -82,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();
}
});
}
});
}
};
Expand Down

0 comments on commit b639c77

Please sign in to comment.