-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.js
More file actions
67 lines (62 loc) · 1.61 KB
/
Copy pathgenerate.js
File metadata and controls
67 lines (62 loc) · 1.61 KB
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
var jsonfile = require('jsonfile');
var jsf = require('json-schema-faker');
var schemaDir = 'duck-raml/schemas';
var collections = {
"users": {
"file": "user-collection.json",
"minItems": 2,
"maxItems": 4
},
"incomeCategories": {
"file": "category-collection.json",
"minItems": 5,
"maxItems": 10
},
"outcomeCategories": {
"file": "category-collection.json",
"minItems": 10,
"maxItems": 30
},
"incomes": {
"file": "income-collection.json",
"minItems": 200,
"maxItems": 500
},
"outcomes": {
"file": "income-collection.json",
"minItems": 1000,
"maxItems": 3000
}
};
var outputFile = "dist/db.json";
var outputData = {};
var promises = [];
function jsonClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
Object.keys(collections).forEach((collectionName) => {
var collectionData = collections[collectionName];
var dfd = Promise.defer();
promises.push(dfd.promise);
var path = schemaDir + "/" + collectionData.file;
jsonfile.readFile(path, function(err, originalJSONSchema) {
var JSONSchema = jsonClone(originalJSONSchema);
JSONSchema.minItems = collectionData.minItems;
JSONSchema.maxItems = collectionData.maxItems;
var tmpCollection = jsf(JSONSchema);
tmpCollection.forEach((el, ind) => {
el.id = ind + 1;
});
outputData[collectionName] = tmpCollection;
dfd.resolve();
});
});
Promise.all(promises).then(() => {
jsonfile.writeFile(outputFile, outputData, {spaces: 2}, function(err) {
if (err) {
console.error(err);
} else {
console.info("Generating", outputFile, "finished");
}
});
});