Skip to content

Commit

Permalink
cli(add): write configuration to yeoman file (#348)
Browse files Browse the repository at this point in the history
* misc(add): variable parity, prettify

* cli(add): write config to yeoman-rc

* misc(add): improve generator questions
  • Loading branch information
dhruvdutt authored and ematipico committed Apr 15, 2018
1 parent df2a92f commit 12ee2bf
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions lib/generators/add-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const entryQuestions = require("./utils/entry");
* Replaces the string with a substring at the given index
* https://gist.github.com/efenacigiray/9367920
*
* @param {String} string - string to be modified
* @param {Number} index - index to replace from
* @param {String} replace - string to replace starting from index
* @param {String} string - string to be modified
* @param {Number} index - index to replace from
* @param {String} replace - string to replace starting from index
*
* @returns {String} string - The newly mutated string
* @returns {String} string - The newly mutated string
*
*/
function replaceAt(string, index, replace) {
Expand All @@ -33,10 +33,10 @@ function replaceAt(string, index, replace) {
*
* Checks if the given array has a given property
*
* @param {Array} arr - array to check
* @param {String} prop - property to check existence of
* @param {Array} arr - array to check
* @param {String} prop - property to check existence of
*
* @returns {Boolean} hasProp - Boolean indicating if the property
* @returns {Boolean} hasProp - Boolean indicating if the property
* is present
*/
const traverseAndGetProperties = (arr, prop) => {
Expand All @@ -52,9 +52,9 @@ const traverseAndGetProperties = (arr, prop) => {
/**
*
* Generator for adding properties
* @class AddGenerator
* @extends Generator
* @returns {Void} After execution, transforms are triggered
* @class AddGenerator
* @extends Generator
* @returns {Void} After execution, transforms are triggered
*
*/

Expand All @@ -65,7 +65,9 @@ module.exports = class AddGenerator extends Generator {
this.configuration = {
config: {
webpackOptions: {},
topScope: ["const webpack = require('webpack')"]
topScope: [
"const webpack = require('webpack')"
]
}
};
}
Expand All @@ -75,7 +77,8 @@ module.exports = class AddGenerator extends Generator {
let action;
let self = this;
let manualOrListInput = action =>
Input("actionAnswer", `what do you want to add to ${action}?`);
Input("actionAnswer", `What do you want to add to ${action}?`);

// first index indicates if it has a deep prop, 2nd indicates what kind of
let isDeepProp = [false, false];

Expand Down Expand Up @@ -104,7 +107,7 @@ module.exports = class AddGenerator extends Generator {
return entryQuestions(self, entryTypeAnswer);
})
.then(entryOptions => {
this.configuration.config.webpackOptions[action] = entryOptions;
this.configuration.config.webpackOptions.entry = entryOptions;
this.configuration.config.item = action;
});
}
Expand Down Expand Up @@ -219,7 +222,7 @@ module.exports = class AddGenerator extends Generator {
}
manualOrListInput = List(
"actionAnswer",
`what do you want to add to ${action}?`,
`What do you want to add to ${action}?`,
Object.keys(defOrPropDescription)
);
// We know we're gonna append some deep prop like module.rule
Expand All @@ -234,7 +237,7 @@ module.exports = class AddGenerator extends Generator {
);
manualOrListInput = List(
"actionAnswer",
`what do you want to add to ${action}?`,
`What do you want to add to ${action}?`,
Object.keys(webpackDevserverSchemaProp.properties)
);
// We know we are in a devServer.prop scenario
Expand Down Expand Up @@ -395,18 +398,18 @@ module.exports = class AddGenerator extends Generator {
// Either we are adding directly at the property, else we're in a prop.theOne scenario
const actionMessage =
isDeepProp[1] === "other"
? `what do you want the key on ${action} to be? (press enter if you want it directly as a value on the property)`
: `what do you want the value of ${isDeepProp[1]} to be?`;
? `What do you want the key on ${action} to be? (press enter if you want it directly as a value on the property)`
: `What do you want the value of ${isDeepProp[1]} to be?`;

this.prompt([Input("deepProp", actionMessage)]).then(
deepPropAns => {
// The other option needs to be validated of either being empty or not
if (isDeepProp[1] === "other") {
let othersDeepPropKey = deepPropAns.deepProp
? `what do you want the value of ${
? `What do you want the value of ${
deepPropAns.deepProp
} to be?` // eslint-disable-line
: `what do you want to be the value of ${action} to be?`;
: `What do you want to be the value of ${action} to be?`;
// Push the answer to the array we have created, so we can use it later
isDeepProp.push(deepPropAns.deepProp);
this.prompt([Input("deepProp", othersDeepPropKey)]).then(
Expand Down Expand Up @@ -447,4 +450,8 @@ module.exports = class AddGenerator extends Generator {
}
});
}

writing() {
this.config.set("configuration", this.configuration);
}
};

0 comments on commit 12ee2bf

Please sign in to comment.