-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
124 lines (97 loc) · 3.41 KB
/
index.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const recast = require('recast');
const { readFileSync, writeFileSync } = require('fs');
const { builders } = recast.types;
function buildKey(configKey) {
return configKey.includes('-') ? builders.literal(configKey) : builders.identifier(configKey);
}
function buildValue(configObject) {
if (typeof configObject === 'object') {
// eslint-disable-next-line arrow-body-style
return builders.objectExpression(Object.keys(configObject).map((objKey) => {
const currentValue = configObject[objKey];
let currentValueNode;
if (Array.isArray(currentValue)) {
currentValueNode = builders.arrayExpression(
currentValue.map((item) => buildValue(item)),
);
} else {
currentValueNode = builders.literal(currentValue);
}
return builders.property('init', builders.identifier(objKey), currentValueNode);
}));
}
return builders.literal(configObject);
}
function applyConfig(project, configKey, configObject, override = false) {
let configFile = './config/environment.js';
if (project.isEmberCLIAddon()) {
configFile = './tests/dummy/config/environment.js';
}
const config = readFileSync(configFile);
const configAst = recast.parse(config);
const key = buildKey(configKey);
const value = buildValue(configObject);
recast.visit(configAst, {
// eslint-disable-next-line consistent-return
visitVariableDeclaration(path) {
const { node } = path;
const env = node.declarations.find((declaration) => declaration.id.name === 'ENV');
if (env) {
let configFileObj = env.init.properties.find(
(property) => property.key.value === configKey || property.key.name === configKey,
);
if (!configFileObj) {
configFileObj = builders.property(
'init',
key,
value,
);
env.init.properties.push(configFileObj);
} else if (override) {
configFileObj.value = value;
}
return false;
}
this.traverse(path);
},
});
writeFileSync(configFile, recast.print(configAst, { tabWidth: 2, quote: 'single' }).code);
}
function applyBuildConfig(configKey, configObject, override = false) {
const configFile = './ember-cli-build.js';
const config = readFileSync(configFile);
const configAst = recast.parse(config);
const key = buildKey(configKey);
const value = buildValue(configObject);
recast.visit(configAst, {
// eslint-disable-next-line consistent-return
visitNewExpression(path) {
const { node } = path;
if (node.callee.name === 'EmberApp'
|| node.callee.name === 'EmberAddon') {
// console.log(node, node.arguments)
const configNode = node.arguments.find((element) => element.type === 'ObjectExpression');
let configFileObj = configNode.properties.find(
(property) => property.key.value === configKey || property.key.name === configKey,
);
if (!configFileObj) {
configFileObj = builders.property(
'init',
key,
value,
);
configNode.properties.push(configFileObj);
} else if (override) {
configFileObj.value = value;
}
return false;
}
this.traverse(path);
},
});
writeFileSync(configFile, recast.print(configAst, { tabWidth: 2, quote: 'single' }).code);
}
module.exports = {
applyConfig,
applyBuildConfig,
};