-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
entry.js
85 lines (82 loc) · 2.34 KB
/
entry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"use strict";
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
*/
module.exports = function entryTransform(j, ast, webpackProperties, action) {
function createEntryProperty(p) {
if (typeof webpackProperties === "string") {
return utils.pushCreateProperty(j, p, "entry", webpackProperties);
}
if (Array.isArray(webpackProperties)) {
const externalArray = utils.createArrayWithChildren(
j,
"entry",
webpackProperties,
true
);
return p.value.properties.push(externalArray);
} else {
utils.pushCreateProperty(j, p, "entry", j.objectExpression([]));
return utils.pushObjectKeys(j, p, webpackProperties, "entry");
}
}
if (webpackProperties) {
if (action === "init") {
return ast
.find(j.ObjectExpression)
.filter(p => utils.isAssignment(null, p, createEntryProperty));
} else if (action === "add") {
const entryNode = utils.findRootNodesByName(j, ast, "entry");
if (entryNode.size() !== 0 && typeof webpackProperties === "object") {
return ast
.find(j.ObjectExpression)
.filter(
p =>
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
"entry"
)
.filter(p => {
Object.keys(webpackProperties).forEach(prop => {
utils.checkIfExistsAndAddValue(
j,
p,
prop,
utils.createIdentifierOrLiteral(j, webpackProperties[prop])
);
});
return ast;
});
} else if (entryNode.size() !== 0 && webpackProperties.length) {
return ast
.find(j.ObjectExpression)
.filter(
p =>
utils.safeTraverse(p, ["parentPath", "value", "key", "name"]) ===
"entry"
)
.forEach(p => {
j(p).replaceWith(
utils.createIdentifierOrLiteral(j, webpackProperties)
);
});
} else {
return entryTransform(j, ast, webpackProperties, "init");
}
} else if (action === "remove") {
// TODO
} else if (action === "update") {
// TODO
}
} else {
return ast;
}
};