-
Notifications
You must be signed in to change notification settings - Fork 39
/
resolve.js
46 lines (38 loc) · 1.14 KB
/
resolve.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
#!/usr/bin/env node
'use strict';
var resolve = require('json-refs').resolveRefs;
var YAML = require('js-yaml');
var fs = require('fs');
var program = require('commander');
program
.version('2.0.0')
.option('-o --output-format [output]',
'output format. Choices are "json" and "yaml" (Default is json)',
'json')
.usage('[options] <yaml file ...>')
.parse(process.argv);
if (program.outputFormat !== 'json' && program.outputFormat !== 'yaml') {
console.error(program.help());
process.exit(1);
}
var file = program.args[0];
if (!fs.existsSync(file)) {
console.error('File does not exist. ('+file+')');
process.exit(1);
}
var root = YAML.safeLoad(fs.readFileSync(file).toString());
var options = {
filter : ['relative', 'remote'],
loaderOptions : {
processContent : function (res, callback) {
callback(null, YAML.safeLoad(res.text));
}
}
};
resolve(root, options).then(function (results) {
if (program.outputFormat === 'yaml') {
console.log(YAML.safeDump(results.resolved));
} else if (program.outputFormat === 'json') {
console.log(JSON.stringify(results.resolved, null, 2));
}
});