-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuildBundle.js
executable file
·55 lines (49 loc) · 1.52 KB
/
buildBundle.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
const path = require('path');
const fs = require('fs');
const src = 'templates';
const dist = 'bundle.json';
const templates = [];
const idsInTemplates = new Set();
const profileDirs = fs.readdirSync(src);
const loadGroup = function(dir, src) {
if (src.items) {
src.items.forEach(function(child) {
if (typeof child === 'string') {
loadItem(dir, child);
} else {
loadGroup(dir, child);
}
});
} else if (src.extends) {
loadItem(dir, src.extends);
}
};
const loadItem = function(dir, id, origId) {
const itemPath = path.join(dir, id);
try {
const src = JSON.parse(fs.readFileSync(itemPath));
src.id = origId || id;
templates.push(src);
idsInTemplates.add(src.id);
loadGroup(dir, src);
} catch (e) {
if (!idsInTemplates.has(origId || id) && profileDirs.indexOf(origId || id) === -1) {
console.log(`Failed loading template with id ${origId || id} in directory ${dir} and it has not been loaded already`);
console.log(itemPath);
}
// Ignore, item in other folder.
}
};
profileDirs.sort((a, b) => (a === 'common' ? 1 : b<a) ).forEach(function (profile) {
if (profile === 'commons') {
const commonPath = path.join(src, profile);
const commonTemplates = fs.readdirSync(commonPath);
commonTemplates.forEach(function(id) {
loadItem(commonPath, id);
});
} else {
const profileDir = path.join(src, profile);
loadItem(profileDir, 'index', profile);
}
});
fs.writeFileSync(dist, JSON.stringify({ templates }, null, ' '));