Skip to content

Commit 28680c9

Browse files
committedDec 17, 2017
add tests for others and new transforms
1 parent b416d9c commit 28680c9

20 files changed

+921
-64
lines changed
 

‎lib/init/transformations/devServer/devServer.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ module.exports = function devServerTransform(
6161
});
6262
return ast;
6363
});
64-
} else if (
65-
devServerNode.size() !== 0 &&
66-
typeof webpackProperties.length
67-
) {
64+
} else if (devServerNode.size() !== 0 && webpackProperties.length) {
6865
return ast
6966
.find(j.ObjectExpression)
7067
.filter(

‎lib/init/transformations/entry/entry.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = function entryTransform(j, ast, webpackProperties, action) {
5757
});
5858
return ast;
5959
});
60-
} else if (entryNode.size() !== 0 && typeof webpackProperties.length) {
60+
} else if (entryNode.size() !== 0 && webpackProperties.length) {
6161
return ast
6262
.find(j.ObjectExpression)
6363
.filter(

‎lib/init/transformations/externals/externals.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ module.exports = function externalsTransform(
8181
Array.isArray(webpackProperties)
8282
) {
8383
// Todo
84-
} else if (externalNode.size() !== 0 && typeof webpackProperties.length) {
84+
} else if (externalNode.size() !== 0 && webpackProperties.length) {
8585
return ast
8686
.find(j.ObjectExpression)
8787
.filter(

‎lib/init/transformations/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ const bailTransform = require("./other/bail");
2424
const cacheTransform = require("./other/cache");
2525
const profileTransform = require("./other/profile");
2626
const mergeTransform = require("./other/merge");
27+
const parallelismTransform = require("./other/parallelism");
28+
const recordsInputPathTransform = require("./other/recordsInputPath");
29+
const recordsOutputPathTransform = require("./other/recordsOutputPath");
30+
const recordsPathTransform = require("./other/recordsPath");
2731
const moduleTransform = require("./module/module");
2832
const pluginsTransform = require("./plugins/plugins");
2933
const topScopeTransform = require("./top-scope/top-scope");
@@ -62,7 +66,11 @@ const transformsObject = {
6266
topScopeTransform,
6367
mergeTransform,
6468
devServerTransform,
65-
modeTransform
69+
modeTransform,
70+
parallelismTransform,
71+
recordsInputPathTransform,
72+
recordsOutputPathTransform,
73+
recordsPathTransform
6674
};
6775

6876
module.exports = function runTransform(webpackProperties, action) {

‎lib/init/transformations/other/__snapshots__/other.test.js.snap

+237
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ exports[`amd transforms correctly using "other-0" data 1`] = `
1616
"
1717
`;
1818

19+
exports[`amd transforms correctly using "other-1" data 1`] = `
20+
"module.exports = {
21+
bail: true,
22+
cache: true,
23+
profile: true,
24+
merge: 'NotMuch',
25+
parallelism: 69,
26+
recordsInputPath: 'somethign',
27+
recordsOutputPath: 'else',
28+
recordsPath: 'Brooklyn',
29+
amd: {
30+
jQuery: false,
31+
kQuery: true
32+
}
33+
};
34+
"
35+
`;
36+
1937
exports[`bail transforms correctly using "other-0" data 1`] = `
2038
"module.exports = {
2139
entry: 'index.js',
@@ -29,6 +47,25 @@ exports[`bail transforms correctly using "other-0" data 1`] = `
2947
"
3048
`;
3149

50+
exports[`bail transforms correctly using "other-1" data 1`] = `
51+
"module.exports = {
52+
bail: false,
53+
cache: true,
54+
profile: true,
55+
merge: 'NotMuch',
56+
parallelism: 69,
57+
recordsInputPath: 'somethign',
58+
recordsOutputPath: 'else',
59+
recordsPath: 'Brooklyn',
60+
amd: {
61+
jQuery: true,
62+
kQuery: false,
63+
bail: false
64+
}
65+
};
66+
"
67+
`;
68+
3269
exports[`cache transforms correctly using "other-0" data 1`] = `
3370
"module.exports = {
3471
entry: 'index.js',
@@ -55,6 +92,44 @@ exports[`cache transforms correctly using "other-0" data 2`] = `
5592
"
5693
`;
5794

95+
exports[`cache transforms correctly using "other-1" data 1`] = `
96+
"module.exports = {
97+
bail: true,
98+
cache: false,
99+
profile: true,
100+
merge: 'NotMuch',
101+
parallelism: 69,
102+
recordsInputPath: 'somethign',
103+
recordsOutputPath: 'else',
104+
recordsPath: 'Brooklyn',
105+
amd: {
106+
jQuery: true,
107+
kQuery: false,
108+
cache: false
109+
}
110+
};
111+
"
112+
`;
113+
114+
exports[`cache transforms correctly using "other-1" data 2`] = `
115+
"module.exports = {
116+
bail: true,
117+
cache: cacheKey,
118+
profile: true,
119+
merge: 'NotMuch',
120+
parallelism: 69,
121+
recordsInputPath: 'somethign',
122+
recordsOutputPath: 'else',
123+
recordsPath: 'Brooklyn',
124+
amd: {
125+
jQuery: true,
126+
kQuery: false,
127+
cache: cacheKey
128+
}
129+
};
130+
"
131+
`;
132+
58133
exports[`merge transforms correctly using "other-0" data 1`] = `
59134
"module.exports = merge(myConfig, {
60135
entry: 'index.js',
@@ -65,6 +140,56 @@ exports[`merge transforms correctly using "other-0" data 1`] = `
65140
"
66141
`;
67142

143+
exports[`merge transforms correctly using "other-1" data 1`] = `
144+
"module.exports = merge(TheirConfig, {
145+
bail: true,
146+
cache: true,
147+
profile: true,
148+
merge: 'NotMuch',
149+
parallelism: 69,
150+
recordsInputPath: 'somethign',
151+
recordsOutputPath: 'else',
152+
recordsPath: 'Brooklyn',
153+
amd: {
154+
jQuery: true,
155+
kQuery: false
156+
}
157+
});
158+
"
159+
`;
160+
161+
exports[`parallelism transforms correctly using "other-0" data 1`] = `
162+
"module.exports = {
163+
entry: 'index.js',
164+
165+
output: {
166+
filename: 'bundle.js'
167+
},
168+
169+
parallelism: 10
170+
}
171+
"
172+
`;
173+
174+
exports[`parallelism transforms correctly using "other-1" data 1`] = `
175+
"module.exports = {
176+
bail: true,
177+
cache: true,
178+
profile: true,
179+
merge: 'NotMuch',
180+
parallelism: 20,
181+
recordsInputPath: 'somethign',
182+
recordsOutputPath: 'else',
183+
recordsPath: 'Brooklyn',
184+
amd: {
185+
jQuery: true,
186+
kQuery: false,
187+
parallelism: 20
188+
}
189+
};
190+
"
191+
`;
192+
68193
exports[`profile transforms correctly using "other-0" data 1`] = `
69194
"module.exports = {
70195
entry: 'index.js',
@@ -77,3 +202,115 @@ exports[`profile transforms correctly using "other-0" data 1`] = `
77202
}
78203
"
79204
`;
205+
206+
exports[`profile transforms correctly using "other-1" data 1`] = `
207+
"module.exports = {
208+
bail: true,
209+
cache: true,
210+
profile: false,
211+
merge: 'NotMuch',
212+
parallelism: 69,
213+
recordsInputPath: 'somethign',
214+
recordsOutputPath: 'else',
215+
recordsPath: 'Brooklyn',
216+
amd: {
217+
jQuery: true,
218+
kQuery: false,
219+
profile: false
220+
}
221+
};
222+
"
223+
`;
224+
225+
exports[`recordsInputPath transforms correctly using "other-0" data 1`] = `
226+
"module.exports = {
227+
entry: 'index.js',
228+
229+
output: {
230+
filename: 'bundle.js'
231+
},
232+
233+
recordsInputPath: path.join('dist', mine)
234+
}
235+
"
236+
`;
237+
238+
exports[`recordsInputPath transforms correctly using "other-1" data 1`] = `
239+
"module.exports = {
240+
bail: true,
241+
cache: true,
242+
profile: true,
243+
merge: 'NotMuch',
244+
parallelism: 69,
245+
recordsInputPath: path.join('dist', ours),
246+
recordsOutputPath: 'else',
247+
recordsPath: 'Brooklyn',
248+
amd: {
249+
jQuery: true,
250+
kQuery: false
251+
}
252+
};
253+
"
254+
`;
255+
256+
exports[`recordsOutputPath transforms correctly using "other-0" data 1`] = `
257+
"module.exports = {
258+
entry: 'index.js',
259+
260+
output: {
261+
filename: 'bundle.js'
262+
},
263+
264+
recordsOutputPath: path.join('src', yours)
265+
}
266+
"
267+
`;
268+
269+
exports[`recordsOutputPath transforms correctly using "other-1" data 1`] = `
270+
"module.exports = {
271+
bail: true,
272+
cache: true,
273+
profile: true,
274+
merge: 'NotMuch',
275+
parallelism: 69,
276+
recordsInputPath: 'somethign',
277+
recordsOutputPath: path.join('src', theirs),
278+
recordsPath: 'Brooklyn',
279+
amd: {
280+
jQuery: true,
281+
kQuery: false
282+
}
283+
};
284+
"
285+
`;
286+
287+
exports[`recordsPath transforms correctly using "other-0" data 1`] = `
288+
"module.exports = {
289+
entry: 'index.js',
290+
291+
output: {
292+
filename: 'bundle.js'
293+
},
294+
295+
recordsPath: path.join(__dirname, 'records.json')
296+
}
297+
"
298+
`;
299+
300+
exports[`recordsPath transforms correctly using "other-1" data 1`] = `
301+
"module.exports = {
302+
bail: true,
303+
cache: true,
304+
profile: true,
305+
merge: 'NotMuch',
306+
parallelism: 69,
307+
recordsInputPath: 'somethign',
308+
recordsOutputPath: 'else',
309+
recordsPath: path.resolve(__dirname, 'gradle.json'),
310+
amd: {
311+
jQuery: true,
312+
kQuery: false
313+
}
314+
};
315+
"
316+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
bail: true,
3+
cache: true,
4+
profile: true,
5+
merge: 'NotMuch',
6+
parallelism: 69,
7+
recordsInputPath: 'somethign',
8+
recordsOutputPath: 'else',
9+
recordsPath: 'Brooklyn',
10+
amd: {
11+
jQuery: true,
12+
kQuery: false
13+
}
14+
};

‎lib/init/transformations/other/amd.js

+61-10
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,70 @@ const utils = require("../../../utils/ast-utils");
1313
* @returns ast - jscodeshift API
1414
*/
1515

16-
module.exports = function(j, ast, webpackProperties, action) {
17-
function createAMDProperty(p) {
16+
module.exports = function amdTransform(j, ast, webpackProperties, action) {
17+
function createAmdProperty(p) {
1818
utils.pushCreateProperty(j, p, "amd", j.objectExpression([]));
1919
return utils.pushObjectKeys(j, p, webpackProperties, "amd");
2020
}
21-
if (
22-
webpackProperties &&
23-
action === "init" &&
24-
typeof webpackProperties === "object"
25-
) {
26-
return ast
27-
.find(j.ObjectExpression)
28-
.filter(p => utils.isAssignment(null, p, createAMDProperty));
21+
if (webpackProperties) {
22+
if (action === "init" && typeof webpackProperties === "object") {
23+
return ast
24+
.find(j.ObjectExpression)
25+
.filter(p => utils.isAssignment(null, p, createAmdProperty));
26+
} else if (action === "init" && webpackProperties.length) {
27+
return ast
28+
.find(j.ObjectExpression)
29+
.filter(p =>
30+
utils.isAssignment(
31+
j,
32+
p,
33+
utils.pushCreateProperty,
34+
"amd",
35+
webpackProperties
36+
)
37+
);
38+
} else if (action === "add") {
39+
const amdNode = utils.findRootNodesByName(j, ast, "amd");
40+
if (amdNode.size() !== 0 && typeof webpackProperties === "object") {
41+
return ast
42+
.find(j.ObjectExpression)
43+
.filter(
44+
p =>
45+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
46+
"amd"
47+
)
48+
.filter(p => {
49+
Object.keys(webpackProperties).forEach(prop => {
50+
utils.checkIfExistsAndAddValue(
51+
j,
52+
p,
53+
prop,
54+
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
55+
);
56+
});
57+
return ast;
58+
});
59+
} else if (amdNode.size() !== 0 && webpackProperties.length) {
60+
return ast
61+
.find(j.ObjectExpression)
62+
.filter(
63+
p =>
64+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
65+
"amd"
66+
)
67+
.forEach(p => {
68+
j(p).replaceWith(
69+
utils.createIdentifierOrLiteral(j, webpackProperties)
70+
);
71+
});
72+
} else {
73+
return amdTransform(j, ast, webpackProperties, "init");
74+
}
75+
} else if (action === "remove") {
76+
// TODO
77+
} else if (action === "update") {
78+
// TODO
79+
}
2980
} else {
3081
return ast;
3182
}

‎lib/init/transformations/other/bail.js

+34-13
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,40 @@ const utils = require("../../../utils/ast-utils");
1313
* @returns ast - jscodeshift API
1414
*/
1515

16-
module.exports = function(j, ast, webpackProperties, action) {
17-
if (webpackProperties && action === "init") {
18-
return ast
19-
.find(j.ObjectExpression)
20-
.filter(p =>
21-
utils.isAssignment(
22-
j,
23-
p,
24-
utils.pushCreateProperty,
25-
"bail",
26-
webpackProperties
27-
)
28-
);
16+
module.exports = function bailTransform(j, ast, webpackProperties, action) {
17+
if (webpackProperties || typeof webpackProperties === "boolean") {
18+
if (action === "init") {
19+
return ast
20+
.find(j.ObjectExpression)
21+
.filter(p =>
22+
utils.isAssignment(
23+
j,
24+
p,
25+
utils.pushCreateProperty,
26+
"bail",
27+
webpackProperties
28+
)
29+
);
30+
} else if (action === "add") {
31+
if (utils.findRootNodesByName(j, ast, "bail").size() !== 0) {
32+
return ast
33+
.find(j.ObjectExpression)
34+
.filter(p =>
35+
utils.checkIfExistsAndAddValue(
36+
j,
37+
p,
38+
"bail",
39+
utils.createIdentifierOrLiteral(j, webpackProperties)
40+
)
41+
);
42+
} else {
43+
return bailTransform(j, ast, webpackProperties, "init");
44+
}
45+
} else if (action === "remove") {
46+
// TODO
47+
} else if (action === "update") {
48+
// TODO
49+
}
2950
} else {
3051
return ast;
3152
}

‎lib/init/transformations/other/cache.js

+68-13
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,74 @@ const utils = require("../../../utils/ast-utils");
1313
* @returns ast - jscodeshift API
1414
*/
1515

16-
module.exports = function(j, ast, webpackProperties, action) {
17-
if (webpackProperties && action === "init") {
18-
return ast
19-
.find(j.ObjectExpression)
20-
.filter(p =>
21-
utils.isAssignment(
22-
j,
23-
p,
24-
utils.pushCreateProperty,
25-
"cache",
26-
webpackProperties
27-
)
28-
);
16+
module.exports = function cacheTransform(j, ast, webpackProperties, action) {
17+
function createCacheProperty(p) {
18+
utils.pushCreateProperty(j, p, "cache", j.objectExpression([]));
19+
return utils.pushObjectKeys(j, p, webpackProperties, "cache");
20+
}
21+
if (webpackProperties || typeof webpackProperties === "boolean") {
22+
if (action === "init" && typeof webpackProperties === "object") {
23+
return ast
24+
.find(j.ObjectExpression)
25+
.filter(p => utils.isAssignment(null, p, createCacheProperty));
26+
} else if (
27+
action === "init" &&
28+
(webpackProperties.length || typeof webpackProperties === "boolean")
29+
) {
30+
return ast
31+
.find(j.ObjectExpression)
32+
.filter(p =>
33+
utils.isAssignment(
34+
j,
35+
p,
36+
utils.pushCreateProperty,
37+
"cache",
38+
webpackProperties
39+
)
40+
);
41+
} else if (action === "add") {
42+
const cacheNode = utils.findRootNodesByName(j, ast, "cache");
43+
if (cacheNode.size() !== 0 && typeof webpackProperties === "object") {
44+
return ast
45+
.find(j.ObjectExpression)
46+
.filter(
47+
p =>
48+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
49+
"cache"
50+
)
51+
.filter(p => {
52+
Object.keys(webpackProperties).forEach(prop => {
53+
utils.checkIfExistsAndAddValue(
54+
j,
55+
p,
56+
prop,
57+
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
58+
);
59+
});
60+
return ast;
61+
});
62+
} else if (
63+
cacheNode.size() !== 0 &&
64+
(typeof webpackProperties === "boolean" || webpackProperties.length > 0)
65+
) {
66+
return ast
67+
.find(j.ObjectExpression)
68+
.filter(p =>
69+
utils.checkIfExistsAndAddValue(
70+
j,
71+
p,
72+
"cache",
73+
utils.createIdentifierOrLiteral(j, webpackProperties)
74+
)
75+
);
76+
} else {
77+
return cacheTransform(j, ast, webpackProperties, "init");
78+
}
79+
} else if (action === "remove") {
80+
// TODO
81+
} else if (action === "update") {
82+
// TODO
83+
}
2984
} else {
3085
return ast;
3186
}

‎lib/init/transformations/other/merge.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ module.exports = function(j, ast, webpackProperties, action) {
3838
};
3939
p.value.body[bodyLength - 1] = newVal;
4040
}
41-
if (webpackProperties && action === "init") {
41+
if (webpackProperties) {
4242
return ast.find(j.Program).filter(p => createMergeProperty(p));
4343
} else {
4444
return ast;

‎lib/init/transformations/other/other.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,65 @@ defineTest(__dirname, "cache", "other-0", true, "init");
1717
defineTest(__dirname, "cache", "other-0", "cacheVal", "init");
1818
defineTest(__dirname, "profile", "other-0", true, "init");
1919
defineTest(__dirname, "merge", "other-0", "myConfig", "init");
20+
21+
defineTest(__dirname, "parallelism", "other-0", 10, "init");
22+
defineTest(
23+
__dirname,
24+
"recordsInputPath",
25+
"other-0",
26+
"path.join('dist', mine)",
27+
"init"
28+
);
29+
defineTest(
30+
__dirname,
31+
"recordsOutputPath",
32+
"other-0",
33+
"path.join('src', yours)",
34+
"init"
35+
);
36+
defineTest(
37+
__dirname,
38+
"recordsPath",
39+
"other-0",
40+
"path.join(__dirname, 'records.json')",
41+
"init"
42+
);
43+
44+
defineTest(
45+
__dirname,
46+
"amd",
47+
"other-1",
48+
{
49+
jQuery: false,
50+
kQuery: true
51+
},
52+
"add"
53+
);
54+
defineTest(__dirname, "bail", "other-1", false, "add");
55+
defineTest(__dirname, "cache", "other-1", false, "add");
56+
defineTest(__dirname, "cache", "other-1", "cacheKey", "add");
57+
defineTest(__dirname, "profile", "other-1", false, "add");
58+
defineTest(__dirname, "merge", "other-1", "TheirConfig", "add");
59+
60+
defineTest(__dirname, "parallelism", "other-1", 20, "add");
61+
defineTest(
62+
__dirname,
63+
"recordsInputPath",
64+
"other-1",
65+
"path.join('dist', ours)",
66+
"add"
67+
);
68+
defineTest(
69+
__dirname,
70+
"recordsOutputPath",
71+
"other-1",
72+
"path.join('src', theirs)",
73+
"add"
74+
);
75+
defineTest(
76+
__dirname,
77+
"recordsPath",
78+
"other-1",
79+
"path.resolve(__dirname, 'gradle.json')",
80+
"add"
81+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
3+
const utils = require("../../../utils/ast-utils");
4+
5+
/*
6+
*
7+
* Transform for parallelism. Finds the parallelism property from yeoman and creates a
8+
* property based on what the user has given us.
9+
*
10+
* @param j — jscodeshift API
11+
* @param ast - jscodeshift API
12+
* @param { Object } webpackProperties - Object containing transformation rules
13+
* @returns ast - jscodeshift API
14+
*/
15+
16+
module.exports = function parallelismTransform(
17+
j,
18+
ast,
19+
webpackProperties,
20+
action
21+
) {
22+
if (webpackProperties) {
23+
if (action === "init") {
24+
return ast
25+
.find(j.ObjectExpression)
26+
.filter(p =>
27+
utils.isAssignment(
28+
j,
29+
p,
30+
utils.pushCreateProperty,
31+
"parallelism",
32+
webpackProperties
33+
)
34+
);
35+
} else if (action === "add") {
36+
if (utils.findRootNodesByName(j, ast, "parallelism").size() !== 0) {
37+
return ast
38+
.find(j.ObjectExpression)
39+
.filter(p =>
40+
utils.checkIfExistsAndAddValue(
41+
j,
42+
p,
43+
"parallelism",
44+
utils.createIdentifierOrLiteral(j, webpackProperties)
45+
)
46+
);
47+
} else {
48+
return parallelismTransform(j, ast, webpackProperties, "init");
49+
}
50+
} else if (action === "remove") {
51+
// TODO
52+
} else if (action === "update") {
53+
// TODO
54+
}
55+
} else {
56+
return ast;
57+
}
58+
};

‎lib/init/transformations/other/profile.js

+68-13
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,74 @@ const utils = require("../../../utils/ast-utils");
1313
* @returns ast - jscodeshift API
1414
*/
1515

16-
module.exports = function(j, ast, webpackProperties, action) {
17-
if (webpackProperties && action === "init") {
18-
return ast
19-
.find(j.ObjectExpression)
20-
.filter(p =>
21-
utils.isAssignment(
22-
j,
23-
p,
24-
utils.pushCreateProperty,
25-
"profile",
26-
webpackProperties
27-
)
28-
);
16+
module.exports = function profileTransform(j, ast, webpackProperties, action) {
17+
function createProfileProperty(p) {
18+
utils.pushCreateProperty(j, p, "profile", j.objectExpression([]));
19+
return utils.pushObjectKeys(j, p, webpackProperties, "profile");
20+
}
21+
if (webpackProperties || typeof webpackProperties === "boolean") {
22+
if (action === "init" && typeof webpackProperties === "object") {
23+
return ast
24+
.find(j.ObjectExpression)
25+
.filter(p => utils.isAssignment(null, p, createProfileProperty));
26+
} else if (
27+
action === "init" &&
28+
(webpackProperties.length || typeof webpackProperties === "boolean")
29+
) {
30+
return ast
31+
.find(j.ObjectExpression)
32+
.filter(p =>
33+
utils.isAssignment(
34+
j,
35+
p,
36+
utils.pushCreateProperty,
37+
"profile",
38+
webpackProperties
39+
)
40+
);
41+
} else if (action === "add") {
42+
const profileNode = utils.findRootNodesByName(j, ast, "profile");
43+
if (profileNode.size() !== 0 && typeof webpackProperties === "object") {
44+
return ast
45+
.find(j.ObjectExpression)
46+
.filter(
47+
p =>
48+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
49+
"profile"
50+
)
51+
.filter(p => {
52+
Object.keys(webpackProperties).forEach(prop => {
53+
utils.checkIfExistsAndAddValue(
54+
j,
55+
p,
56+
prop,
57+
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
58+
);
59+
});
60+
return ast;
61+
});
62+
} else if (
63+
profileNode.size() !== 0 &&
64+
(typeof webpackProperties === "boolean" || webpackProperties.length > 0)
65+
) {
66+
return ast
67+
.find(j.ObjectExpression)
68+
.filter(p =>
69+
utils.checkIfExistsAndAddValue(
70+
j,
71+
p,
72+
"profile",
73+
utils.createIdentifierOrLiteral(j, webpackProperties)
74+
)
75+
);
76+
} else {
77+
return profileTransform(j, ast, webpackProperties, "init");
78+
}
79+
} else if (action === "remove") {
80+
// TODO
81+
} else if (action === "update") {
82+
// TODO
83+
}
2984
} else {
3085
return ast;
3186
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use strict";
2+
3+
const utils = require("../../../utils/ast-utils");
4+
5+
/*
6+
*
7+
* Transform for recordsInputPath. Finds the recordsInputPath property from yeoman and creates a
8+
* property based on what the user has given us.
9+
*
10+
* @param j — jscodeshift API
11+
* @param ast - jscodeshift API
12+
* @param { Object } webpackProperties - Object containing transformation rules
13+
* @returns ast - jscodeshift API
14+
*/
15+
16+
module.exports = function recordsInputPathTransform(
17+
j,
18+
ast,
19+
webpackProperties,
20+
action
21+
) {
22+
function createRecordsInputPathProperty(p) {
23+
utils.pushCreateProperty(j, p, "recordsInputPath", j.objectExpression([]));
24+
return utils.pushObjectKeys(j, p, webpackProperties, "recordsInputPath");
25+
}
26+
if (webpackProperties) {
27+
if (action === "init" && typeof webpackProperties === "object") {
28+
return ast
29+
.find(j.ObjectExpression)
30+
.filter(p =>
31+
utils.isAssignment(null, p, createRecordsInputPathProperty)
32+
);
33+
} else if (action === "init" && webpackProperties.length) {
34+
return ast
35+
.find(j.ObjectExpression)
36+
.filter(p =>
37+
utils.isAssignment(
38+
j,
39+
p,
40+
utils.pushCreateProperty,
41+
"recordsInputPath",
42+
webpackProperties
43+
)
44+
);
45+
} else if (action === "add") {
46+
const recordsInputPathNode = utils.findRootNodesByName(
47+
j,
48+
ast,
49+
"recordsInputPath"
50+
);
51+
if (
52+
recordsInputPathNode.size() !== 0 &&
53+
typeof webpackProperties === "object"
54+
) {
55+
return ast
56+
.find(j.ObjectExpression)
57+
.filter(
58+
p =>
59+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
60+
"recordsInputPath"
61+
)
62+
.filter(p => {
63+
Object.keys(webpackProperties).forEach(prop => {
64+
utils.checkIfExistsAndAddValue(
65+
j,
66+
p,
67+
prop,
68+
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
69+
);
70+
});
71+
return ast;
72+
});
73+
} else if (
74+
recordsInputPathNode.size() !== 0 &&
75+
webpackProperties.length
76+
) {
77+
return utils
78+
.findRootNodesByName(j, ast, "recordsInputPath")
79+
.forEach(p => {
80+
j(p).replaceWith(
81+
j.property(
82+
"init",
83+
utils.createIdentifierOrLiteral(j, "recordsInputPath"),
84+
utils.createIdentifierOrLiteral(j, webpackProperties)
85+
)
86+
);
87+
});
88+
} else {
89+
return recordsInputPathTransform(j, ast, webpackProperties, "init");
90+
}
91+
} else if (action === "remove") {
92+
// TODO
93+
} else if (action === "update") {
94+
// TODO
95+
}
96+
} else {
97+
return ast;
98+
}
99+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use strict";
2+
3+
const utils = require("../../../utils/ast-utils");
4+
5+
/*
6+
*
7+
* Transform for recordsOutputPath. Finds the recordsOutputPath property from yeoman and creates a
8+
* property based on what the user has given us.
9+
*
10+
* @param j — jscodeshift API
11+
* @param ast - jscodeshift API
12+
* @param { Object } webpackProperties - Object containing transformation rules
13+
* @returns ast - jscodeshift API
14+
*/
15+
16+
module.exports = function recordsOutputPathTransform(
17+
j,
18+
ast,
19+
webpackProperties,
20+
action
21+
) {
22+
function createRecordsOutputPathProperty(p) {
23+
utils.pushCreateProperty(j, p, "recordsOutputPath", j.objectExpression([]));
24+
return utils.pushObjectKeys(j, p, webpackProperties, "recordsOutputPath");
25+
}
26+
if (webpackProperties) {
27+
if (action === "init" && typeof webpackProperties === "object") {
28+
return ast
29+
.find(j.ObjectExpression)
30+
.filter(p =>
31+
utils.isAssignment(null, p, createRecordsOutputPathProperty)
32+
);
33+
} else if (action === "init" && webpackProperties.length) {
34+
return ast
35+
.find(j.ObjectExpression)
36+
.filter(p =>
37+
utils.isAssignment(
38+
j,
39+
p,
40+
utils.pushCreateProperty,
41+
"recordsOutputPath",
42+
webpackProperties
43+
)
44+
);
45+
} else if (action === "add") {
46+
const recordsOutputPathNode = utils.findRootNodesByName(
47+
j,
48+
ast,
49+
"recordsOutputPath"
50+
);
51+
if (
52+
recordsOutputPathNode.size() !== 0 &&
53+
typeof webpackProperties === "object"
54+
) {
55+
return ast
56+
.find(j.ObjectExpression)
57+
.filter(
58+
p =>
59+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
60+
"recordsOutputPath"
61+
)
62+
.filter(p => {
63+
Object.keys(webpackProperties).forEach(prop => {
64+
utils.checkIfExistsAndAddValue(
65+
j,
66+
p,
67+
prop,
68+
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
69+
);
70+
});
71+
return ast;
72+
});
73+
} else if (
74+
recordsOutputPathNode.size() !== 0 &&
75+
webpackProperties.length
76+
) {
77+
return utils
78+
.findRootNodesByName(j, ast, "recordsOutputPath")
79+
.forEach(p => {
80+
j(p).replaceWith(
81+
j.property(
82+
"init",
83+
utils.createIdentifierOrLiteral(j, "recordsOutputPath"),
84+
utils.createIdentifierOrLiteral(j, webpackProperties)
85+
)
86+
);
87+
});
88+
} else {
89+
return recordsOutputPathTransform(j, ast, webpackProperties, "init");
90+
}
91+
} else if (action === "remove") {
92+
// TODO
93+
} else if (action === "update") {
94+
// TODO
95+
}
96+
} else {
97+
return ast;
98+
}
99+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
"use strict";
2+
3+
const utils = require("../../../utils/ast-utils");
4+
5+
/*
6+
*
7+
* Transform for recordsPath. Finds the recordsPath property from yeoman and creates a
8+
* property based on what the user has given us.
9+
*
10+
* @param j — jscodeshift API
11+
* @param ast - jscodeshift API
12+
* @param { Object } webpackProperties - Object containing transformation rules
13+
* @returns ast - jscodeshift API
14+
*/
15+
16+
module.exports = function recordsPathTransform(
17+
j,
18+
ast,
19+
webpackProperties,
20+
action
21+
) {
22+
function createRecordsPathProperty(p) {
23+
utils.pushCreateProperty(j, p, "recordsPath", j.objectExpression([]));
24+
return utils.pushObjectKeys(j, p, webpackProperties, "recordsPath");
25+
}
26+
if (webpackProperties) {
27+
if (action === "init" && typeof webpackProperties === "object") {
28+
return ast
29+
.find(j.ObjectExpression)
30+
.filter(p => utils.isAssignment(null, p, createRecordsPathProperty));
31+
} else if (action === "init" && webpackProperties.length) {
32+
return ast
33+
.find(j.ObjectExpression)
34+
.filter(p =>
35+
utils.isAssignment(
36+
j,
37+
p,
38+
utils.pushCreateProperty,
39+
"recordsPath",
40+
webpackProperties
41+
)
42+
);
43+
} else if (action === "add") {
44+
const recordsPathNode = utils.findRootNodesByName(j, ast, "recordsPath");
45+
if (
46+
recordsPathNode.size() !== 0 &&
47+
typeof webpackProperties === "object"
48+
) {
49+
return ast
50+
.find(j.ObjectExpression)
51+
.filter(
52+
p =>
53+
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
54+
"recordsPath"
55+
)
56+
.filter(p => {
57+
Object.keys(webpackProperties).forEach(prop => {
58+
utils.checkIfExistsAndAddValue(
59+
j,
60+
p,
61+
prop,
62+
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
63+
);
64+
});
65+
return ast;
66+
});
67+
} else if (recordsPathNode.size() !== 0 && webpackProperties.length) {
68+
return utils.findRootNodesByName(j, ast, "recordsPath").forEach(p => {
69+
j(p).replaceWith(
70+
j.property(
71+
"init",
72+
utils.createIdentifierOrLiteral(j, "recordsPath"),
73+
utils.createIdentifierOrLiteral(j, webpackProperties)
74+
)
75+
);
76+
});
77+
} else {
78+
return recordsPathTransform(j, ast, webpackProperties, "init");
79+
}
80+
} else if (action === "remove") {
81+
// TODO
82+
} else if (action === "update") {
83+
// TODO
84+
}
85+
} else {
86+
return ast;
87+
}
88+
};

‎lib/utils/ast-utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,9 @@ function createEmptyCallableFunctionWithArguments(
622622
j: Ijscodeshit,
623623
name: String
624624
): any {
625-
return j.callExpression(j.identifier(name), [j.literal("/* Add your arguments here */")]);
625+
return j.callExpression(j.identifier(name), [
626+
j.literal("/* Add your arguments here */")
627+
]);
626628
}
627629

628630
module.exports = {

‎lib/utils/ast-utils.test.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -324,10 +324,9 @@ var a = { plugs: [] }
324324
});
325325
describe("createEmptyCallableFunctionWithArguments", () => {
326326
it("Should create a empty function with a name", () => {
327-
const ast = j(utils.createEmptyCallableFunctionWithArguments(
328-
j,
329-
"ringoStarr"
330-
));
327+
const ast = j(
328+
utils.createEmptyCallableFunctionWithArguments(j, "ringoStarr")
329+
);
331330
expect(ast.toSource()).toMatchSnapshot();
332331
});
333332
});

‎lib/utils/prop-types.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,14 @@ module.exports = new Set([
1414
"watch",
1515
"watchOptions",
1616
"stats",
17-
"mode"
17+
"mode",
18+
"amd",
19+
"bail",
20+
"cache",
21+
"profile",
22+
"merge",
23+
"parallelism",
24+
"recordsInputPath",
25+
"recordsOutputPath",
26+
"recordsPath"
1827
]);

‎webpack.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
module.exports = {};
1+
const webpack = require('webpack');
2+
module.exports = {
3+
recordsPath: path.join(__dirname, 'gradle')
4+
};

0 commit comments

Comments
 (0)
Please sign in to comment.