This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
config-generator.js
293 lines (230 loc) · 7.24 KB
/
config-generator.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
var beautify = require('js-beautify').js_beautify;
var escodegen = require('escodegen');
var esprima = require('esprima-fb');
var fs = require('fs');
var jsstana = require('jsstana');
var path = require('path');
var pkg = require('./package.json');
var program = require('commander');
var Promise = require('bluebird');
var updateNotifier = require('update-notifier');
var walk = require('walk');
updateNotifier({
packageName: pkg.name,
packageVersion: pkg.version
}).notify();
Promise.promisifyAll(fs);
function list(value) {
return value.split(',').map(String);
}
program
.usage('[options] <file ...>', list)
.option('-c, --config [config object]', 'The configuration object in which the modules should be added.', String, '__CONFIG__')
.option('-o, --output [file name]', 'Output file to store the generated configuration.')
.option('-b, --base [file name]', 'Already existing template base to be joined with the parsed configuration.')
.version(require('./package.json').version)
.parse(process.argv);
var options = {
followLinks: false
};
function extractCondition(ast) {
var found;
var meta = ast;
var values = {};
jsstana.traverse(ast, function (node) {
if (!found) {
var match = jsstana.match('(ident META)', node);
if (match) {
jsstana.traverse(meta, function (node) {
if (!found) {
match = jsstana.match('(return)', node);
if (match) {
values = extractObjectValues(['path', 'fullPath', 'condition', 'group'], node);
found = true;
}
}
});
} else {
meta = node;
}
}
});
return values;
}
function extractObjectValues(idents, ast) {
var result = Object.create(null);
var found;
var ident;
if (ast) {
jsstana.traverse(ast, function (node) {
if (found) {
found = false;
result[ident] = extractValue(node);
}
for (var i = 0; i < idents.length; i++) {
ident = idents[i];
if (jsstana.match('(ident ' + ident + ')', node)) {
found = true;
break;
}
}
});
}
return result;
}
function extractValue(node) {
var i;
var property;
if (node.type === 'Literal') {
return node.value;
} else if (node.type === 'ObjectExpression') {
var obj = {};
for (i = 0; i < node.properties.length; i++) {
property = node.properties[i];
obj[property.key.name] = extractValue(property.value);
}
return obj;
} else if (node.type === 'ArrayExpression') {
var arr = [];
for (i = 0; i < node.elements.length; i++) {
arr.push(extractValue(node.elements[i]));
}
return arr;
} else if (node.type === 'FunctionExpression') {
return escodegen.generate(node);
}
}
function generateConfig() {
return new Promise(function(resolve, reject) {
var config = {};
for (var i = 0; i < modules.length; i++) {
var module = modules[i];
var storedModule = config[module.name] = {
dependencies: module.dependencies
};
if (module.condition) {
storedModule.condition = module.condition;
}
if (module.fullPath) {
storedModule.fullPath = module.fullPath;
}
else {
var dirname = path.dirname(module.name);
var modulePath = module.path || (dirname !== '.' ? dirname + '/' + module.file : module.file);
storedModule.path = modulePath;
}
}
resolve(config);
});
}
function getConfig(file, ast) {
return new Promise(function(resolve, reject) {
var result = [];
jsstana.traverse(ast, function (node) {
var match = jsstana.match('(call define ? ? ?)', node);
if (match) {
var config = {
file: path.basename(file),
name: node.arguments[0].value,
dependencies: extractValue(node.arguments[1])
};
var values = extractCondition(node);
Object.keys(values || {}).forEach(function(key) {
config[key] = values[key];
});
result.push(config);
}
});
resolve(result);
});
}
function parseFile(file, content) {
return new Promise(function(resolve, reject) {
var ast = esprima.parse(content, options);
resolve(ast);
});
}
function processFile(file) {
return new Promise(function(resolve) {
fs.readFileAsync(file, 'utf-8')
.then(function(content) {
return parseFile(file, content);
})
.then(function(ast) {
return getConfig(file, ast);
})
.then(function(config) {
modules = modules.concat(config);
resolve(config);
});
});
}
function onWalkerFile(root, fileStats, next) {
var file = path.join(root, fileStats.name);
var fileExt = file.substr(file.lastIndexOf('.') + 1);
if ('js' === fileExt.toLowerCase()) {
processFile(file)
.then(function(config) {
next();
});
}
else {
next();
}
}
function onWalkerEnd(walker){
return new Promise(function(resolve, reject) {
walker.on('end', resolve);
});
}
function saveConfig(config) {
return new Promise(function(resolve, reject) {
if (program.output) {
fs.writeFileAsync(program.output, config)
.then(function() {
resolve(config);
});
} else {
console.log(config);
}
});
}
var base;
var i;
var modules = [];
var processors = [];
if (program.base) {
base = fs.readFileSync(path.resolve(program.base), 'utf8');
}
// For every file or folder, create a promise,
// parse the file, extract the confing and store it
// in the global modules array.
// Once all files are being processed, store the generated config.
for (i = 0; i < program.args.length; i++) {
var file = program.args[i];
var fileStats = fs.statSync(file);
if (fileStats.isDirectory(file)) {
var walker = walk.walk(file, options);
walker.on('file', onWalkerFile);
processors.push(onWalkerEnd(walker));
} else if(fileStats.isFile()) {
processors.push(processFile(file));
}
}
Promise.all(processors)
.then(function(uselessPromises) {
return generateConfig();
})
.then(function(config) {
var content;
if (base) {
content = base + program.config + '.modules = ' + JSON.stringify(config) + ';';
} else {
content = 'var ' + program.config + ' = {modules: ' + JSON.stringify(config) + '};';
}
return saveConfig(beautify(content));
})
.catch(function(error) {
console.error(error);
});