-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
executable file
·49 lines (44 loc) · 1.49 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
#!/usr/bin/env node
var jsonrefs = require('json-refs');
var yaml = require('yaml-js');
var fs = require('fs');
var swaggerparser = require('swagger-parser');
function compile() {
jsonrefs.clearCache();
var root = yaml.load(fs.readFileSync('swagger.yaml').toString());
var options = {
filter: ['relative', 'remote'],
loaderOptions: {
processContent: function(res, callback) {
callback(null, yaml.load(res.text));
}
}
};
jsonrefs.resolveRefs(root, options)
.then(function(results) {
if (!fs.existsSync('output/')) {
fs.mkdirSync('output/');
}
fs.writeFileSync('output/swagger.json', JSON.stringify(results.resolved, null, 2), null, function(err) {
if (err) {
console.error(err.message);
process.exit(42);
}
});
validate();
});
}
function validate() {
return swaggerparser.validate("output/swagger.json", { validate: { spec: true }, allow: { empty: false}})
.then(function() {
fs.writeFileSync('output/errors.json', '{}');
console.log('All valid.');
})
.catch(function(error) {
fs.writeFileSync('output/errors.json', JSON.stringify(error, null, 2));
console.error('Please correct the error below.');
console.error(error.message);
process.exit(42);
});
}
compile();