forked from keboola/kbc-ui-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
128 lines (109 loc) · 3.47 KB
/
build.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
var fs = require("fs");
var rmdir = require('rmdir-recursive').sync;
var _ = require("lodash");
var resourcesFolder = process.argv[2];
var resources = loadResources(resourcesFolder);
materializeResources(resources, "dist");
// Materialize files in build dir
function materializeResources(resources, dir) {
rmdir(dir, false);
fs.mkdirSync(dir);
_.forEach(resources, function(resource, key) {
console.log("Building", key);
if (!resource.schemas.params) {
resource.schemas.params = {};
}
if (!resource.schemas.params.definitions) {
resource.schemas.params.definitions = {};
}
if (!resource.schemas.api) {
resource.schemas.api = {};
}
if (!resource.schemas.api || !resource.schemas.api.definitions) {
resource.schemas.api.definitions = {};
}
var stringified = JSON.stringify(resource);
fs.writeFileSync(dir + "/" + key + ".json", stringified);
});
}
function loadResources(dir) {
var resources = {};
var list = fs.readdirSync(dir);
// load all directories in resources level
list.forEach(function (file) {
var stat = fs.statSync(dir + '/' + file);
if (stat.isDirectory()) {
resources[file] = {};
var fullPath = dir + '/' + file;
var resourceName = file;
resources[file] = addResource(fullPath, resourceName);
}
});
return resources;
}
function addResource(path, name) {
var data = {
"schemas": addResourceItem(path, name, "schemas"),
"templates": addResourceItem(path, name, "templates")
};
data.templates["config"] = addTemplates(path, name);
// backward compatibility mode
return data;
}
function addTemplates(path, name) {
var data = [];
if (!fs.existsSync(path + "/templates") || !fs.existsSync(path + "/templates/config")) {
return;
}
console.log("Loading", name, "templates");
var list = fs.readdirSync(path + "/templates/config")
if (!list) {
return data;
}
list.forEach(function (file) {
// skip nonjson files
if (file.substr(-5) != '.json') {
return;
}
var templateName = file.substr(0, file.length - 5);
var templateFilePath = path + "/templates/config/" + templateName + ".json";
var templateMeta = loadJSONFile(templateFilePath);
try {
data.push(templateMeta);
} catch (e) {
throw "Cannot parse " + filePath + ": " + e;
}
});
return data;
}
function addResourceItem(path, name, resourceType) {
var data = {};
if (!fs.existsSync(path + "/" + resourceType)) {
return data;
}
console.log("Loading", name, resourceType);
// load all directories in resources level
var list = fs.readdirSync(path + "/" + resourceType)
if (!list) {
return data;
}
list.forEach(function (file) {
if (file.substr(-5) != '.json') {
return;
}
var schemaName = file.substr(0, file.length - 5);
var filePath = path + "/" + resourceType + "/" + file;
data[schemaName] = loadJSONFile(filePath);
});
return data;
}
function loadJSONFile(file) {
if (file.substr(-5) != '.json') {
throw file + " not a JSON file";
}
try {
return JSON.parse(fs.readFileSync(file, "utf8").trim());
} catch (e) {
throw Error("Cannot parse " + file + ": " + e);
}
}