forked from switch120/serverless-btb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
62 lines (47 loc) · 1.41 KB
/
build.ts
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
const recursive = require("recursive-readdir");
const yaml = require("js-yaml");
const fs = require("fs");
const merge = require("merge-deep");
const BUILD_FOLDER = "./src/compiled-config";
const highOrderSegments = [
"config"
];
fs.mkdirSync(BUILD_FOLDER);
Promise.all(
highOrderSegments.map(s => recursive(s).then(results => {
return {
segment: s,
results: results.filter(r => !r.match(/\.gitkeep/))
};
}))
).then(segments => {
segments.forEach(({ segment, results }) => {
let segmentData: any = {};
results.forEach(result => {
const [, ...pathSegments] = result.split("/");
const file = pathSegments.pop();
console.log(`Parsing ${file}`);
try {
const doc = yaml.safeLoad(fs.readFileSync(result, 'utf8'));
pathSegments.push(file.split(".")[0]);
let tmpObj = recurse(pathSegments.join("."), {
path: pathSegments.join("/"),
file: file,
parsed: doc
});
// deep merge the new object with the existing
segmentData = merge(segmentData, tmpObj);
} catch (e) {
console.log(e);
}
});
console.log(segmentData);
fs.writeFileSync(`${BUILD_FOLDER}/${segment}.json`, JSON.stringify(segmentData, null, 2));
});
});
function recurse(str, obj) {
const tmp = str.split(".");
return {
[tmp[0]]: tmp.length > 1 ? recurse(tmp.slice(1).join("."), obj) : obj
}
}