forked from hugolantaume/reqres
-
Notifications
You must be signed in to change notification settings - Fork 14
/
compile_data.js
43 lines (36 loc) · 1.01 KB
/
compile_data.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
var fs = require("fs"),
_ = require("lodash");
function extractData(filename) {
if (filename.match(/.*\.json$/)) { //check for json file
content = fs.readFileSync('resources/' + filename);
resource_name = filename.match(/(.*)\.json$/)[1]
result = {}
result[resource_name] = JSON.parse(content); // let the JSON parsing errors be raised
return result;
} else {
return {};
}
};
fs.readdir("resources/", function(err, files) {
if (err) {
return console.error(err);
}
data = {};
console.log(files);
contents = files.map(function(file) {
return extractData(file);
});
contents.forEach(function(content) {
keys = _.keys(content);
if (keys.length == 1 && !_.has(data, keys[0])) { // 1 resource per file and no resource should have multiple files
_.merge(data, content);
} else {
console.error("Invalid file");
}
});
fs.writeFile("data.json", JSON.stringify(data), function(err) {
if (err) {
return console.error(err);
}
});
});