Skip to content

Commit

Permalink
cli(entry): quotes sanitization (#337)
Browse files Browse the repository at this point in the history
* chore: minor doc fixes

* cli(entry): quotes sanitization

* tests(entry): add test case for double quotes

* tests(entry): update snapshots

* misc(utils): entry - variable parity

* misc(prop-types): sort

* cli(entry): multiple entries quotes sanitization
  • Loading branch information
dhruvdutt authored and evenstensberg committed Apr 17, 2018
1 parent f42bdbd commit 0354db8
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 48 deletions.
4 changes: 2 additions & 2 deletions lib/commands/init.js
Expand Up @@ -9,9 +9,9 @@ const modifyHelper = require("../utils/modify-config-helper");
* First function to be called after running the init flag. This is a check,
* if we are running the init command with no arguments or if we got dependencies
*
* @param {Array} args - array of arguments such as
* @param {Array} args - array of arguments such as
* packages included when running the init command
* @returns {Function} creator/npmPackagesExists - returns an installation of the package,
* @returns {Function} creator/npmPackagesExists - returns an installation of the package,
* followed up with a yeoman instance of that if there's packages. If not, it creates a defaultGenerator
*/

Expand Down
34 changes: 19 additions & 15 deletions lib/generators/utils/entry.js
Expand Up @@ -7,9 +7,9 @@ const validate = require("./validate");
*
* Prompts for entry points, either if it has multiple or one entry
*
* @param {Object} self - A variable holding the instance of the prompting
* @param {Object} answer - Previous answer from asking if the user wants single or multiple entries
* @returns {Object} An Object that holds the answers given by the user, later used to scaffold
* @param {Object} self - A variable holding the instance of the prompting
* @param {Object} answer - Previous answer from asking if the user wants single or multiple entries
* @returns {Object} An Object that holds the answers given by the user, later used to scaffold
*/

module.exports = (self, answer) => {
Expand All @@ -28,7 +28,7 @@ module.exports = (self, answer) => {
let webpackEntryPoint = {};
entryIdentifiers = multipleEntriesAnswer["multipleEntries"].split(",");
function forEachPromise(obj, fn) {
return obj.reduce(function(promise, prop) {
return obj.reduce((promise, prop) => {
const trimmedProp = prop.trim();
return promise.then(n => {
if (n) {
Expand All @@ -40,7 +40,7 @@ module.exports = (self, answer) => {
!n[val].includes("path") &&
!n[val].includes("process")
) {
n[val] = `"'${n[val]}.js'"`;
n[val] = `\'${n[val].replace(/"|'/g,"").concat(".js")}\'`;
}
webpackEntryPoint[val] = n[val];
});
Expand All @@ -59,18 +59,18 @@ module.exports = (self, answer) => {
validate
)
])
).then(propAns => {
Object.keys(propAns).forEach(val => {
).then(entryPropAnswer => {
Object.keys(entryPropAnswer).forEach(val => {
if (
propAns[val].charAt(0) !== "(" &&
propAns[val].charAt(0) !== "[" &&
!propAns[val].includes("function") &&
!propAns[val].includes("path") &&
!propAns[val].includes("process")
entryPropAnswer[val].charAt(0) !== "(" &&
entryPropAnswer[val].charAt(0) !== "[" &&
!entryPropAnswer[val].includes("function") &&
!entryPropAnswer[val].includes("path") &&
!entryPropAnswer[val].includes("process")
) {
propAns[val] = `"'${propAns[val]}.js'"`;
entryPropAnswer[val] = `\'${entryPropAnswer[val].replace(/"|'/g,"").concat(".js")}\'`;
}
webpackEntryPoint[val] = propAns[val];
webpackEntryPoint[val] = entryPropAnswer[val];
});
return webpackEntryPoint;
});
Expand All @@ -83,7 +83,11 @@ module.exports = (self, answer) => {
"Which module will be the first to enter the application? [default: ./src/index]"
)
])
.then(singularAnswer => `"${singularAnswer["singularEntry"]}"`);
.then(singularEntryAnswer => {
let { singularEntry } = singularEntryAnswer;
if (singularEntry.indexOf("\"") >= 0) singularEntry = singularEntry.replace(/"/g, "'");
return singularEntry;
});
}
return result;
};
8 changes: 4 additions & 4 deletions lib/init/index.js
Expand Up @@ -72,8 +72,8 @@ const transformsObject = {
* Maps back transforms that needs to be run using the configuration
* provided.
*
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @param {Object} transformObject - An Object with all transformations
* @param {Object} config - Configuration to transform
* @returns {Object} - An Object with the transformations to be run
*/

Expand Down Expand Up @@ -104,8 +104,8 @@ function mapOptionsToTransform(transformObject, config) {
*
* Runs the transformations from an object we get from yeoman
*
* @param {Object} webpackProperties - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @param {Object} webpackProperties - Configuration to transform
* @param {String} action - Action to be done on the given ast
* @returns {Promise} - A promise that writes each transform, runs prettier
* and writes the file
*/
Expand Down
27 changes: 17 additions & 10 deletions lib/init/transformations/entry/__snapshots__/entry.test.js.snap
Expand Up @@ -9,12 +9,19 @@ exports[`entry transforms correctly using "entry-0" data 1`] = `

exports[`entry transforms correctly using "entry-0" data 2`] = `
"module.exports = {
entry: ['index.js', 'app.js']
entry: \\"index.js\\"
}
"
`;

exports[`entry transforms correctly using "entry-0" data 3`] = `
"module.exports = {
entry: ['index.js', 'app.js']
}
"
`;

exports[`entry transforms correctly using "entry-0" data 4`] = `
"module.exports = {
entry: {
index: 'index.js',
Expand All @@ -24,7 +31,7 @@ exports[`entry transforms correctly using "entry-0" data 3`] = `
"
`;

exports[`entry transforms correctly using "entry-0" data 4`] = `
exports[`entry transforms correctly using "entry-0" data 5`] = `
"module.exports = {
entry: {
something,
Expand All @@ -35,42 +42,42 @@ exports[`entry transforms correctly using "entry-0" data 4`] = `
"
`;

exports[`entry transforms correctly using "entry-0" data 5`] = `
exports[`entry transforms correctly using "entry-0" data 6`] = `
"module.exports = {
entry: () => 'index.js'
}
"
`;

exports[`entry transforms correctly using "entry-0" data 6`] = `
exports[`entry transforms correctly using "entry-0" data 7`] = `
"module.exports = {
entry: () => new Promise((resolve) => resolve(['./app', './router']))
}
"
`;

exports[`entry transforms correctly using "entry-0" data 7`] = `
exports[`entry transforms correctly using "entry-0" data 8`] = `
"module.exports = {
entry: entryStringVariable
}
"
`;

exports[`entry transforms correctly using "entry-0" data 8`] = `
exports[`entry transforms correctly using "entry-0" data 9`] = `
"module.exports = {
entry: 'index.js'
}
"
`;

exports[`entry transforms correctly using "entry-0" data 9`] = `
exports[`entry transforms correctly using "entry-0" data 10`] = `
"module.exports = {
entry: ['index.js', 'app.js']
}
"
`;

exports[`entry transforms correctly using "entry-0" data 10`] = `
exports[`entry transforms correctly using "entry-0" data 11`] = `
"module.exports = {
entry: {
something,
Expand All @@ -81,14 +88,14 @@ exports[`entry transforms correctly using "entry-0" data 10`] = `
"
`;

exports[`entry transforms correctly using "entry-0" data 11`] = `
exports[`entry transforms correctly using "entry-0" data 12`] = `
"module.exports = {
entry: () => new Promise((resolve) => resolve(['./app', './router']))
}
"
`;

exports[`entry transforms correctly using "entry-0" data 12`] = `
exports[`entry transforms correctly using "entry-0" data 13`] = `
"module.exports = {
entry: entryStringVariable
}
Expand Down
10 changes: 5 additions & 5 deletions lib/init/transformations/entry/entry.js
Expand Up @@ -7,11 +7,11 @@ const utils = require("../../../utils/ast-utils");
* Transform for entry. Finds the entry property from yeoman and creates a
* property based on what the user has given us.
*
* @param j — jscodeshift API
* @param ast - jscodeshift API
* @param {any} webpackProperties - transformation object to scaffold
* @param {String} action - action that indicates what to be done to the AST
* @returns ast - jscodeshift API
* @param j - jscodeshift API
* @param ast - jscodeshift API
* @param {any} webpackProperties - transformation object to scaffold
* @param {String} action - action that indicates what to be done to the AST
* @returns ast - jscodeshift API
*/

module.exports = function entryTransform(j, ast, webpackProperties, action) {
Expand Down
1 change: 1 addition & 0 deletions lib/init/transformations/entry/entry.test.js
Expand Up @@ -3,6 +3,7 @@
const defineTest = require("../../../utils/defineTest");

defineTest(__dirname, "entry", "entry-0", "'index.js'", "init");
defineTest(__dirname, "entry", "entry-0", "\"index.js\"", "init");
defineTest(__dirname, "entry", "entry-0", ["'index.js'", "'app.js'"], "init");
defineTest(
__dirname,
Expand Down
24 changes: 12 additions & 12 deletions lib/utils/prop-types.js
Expand Up @@ -6,30 +6,30 @@
*/

module.exports = new Set([
"amd",
"bail",
"cache",
"context",
"devServer",
"devtool",
"entry",
"externals",
"merge",
"mode",
"module",
"node",
"output",
"parallelism",
"performance",
"plugins",
"resolve",
"target",
"watch",
"watchOptions",
"stats",
"mode",
"amd",
"bail",
"cache",
"profile",
"merge",
"parallelism",
"recordsInputPath",
"recordsOutputPath",
"recordsPath",
"resolveLoader"
"resolve",
"resolveLoader",
"stats",
"target",
"watch",
"watchOptions",
]);

0 comments on commit 0354db8

Please sign in to comment.