forked from wework/speccy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.js
executable file
·61 lines (53 loc) · 1.75 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const yaml = require('yaml');
const config = require('./lib/config.js');
const loader = require('./lib/loader.js');
const fromJsonSchema = require('json-schema-to-openapi-schema');
const command = async (file, cmd) => {
config.init(cmd);
const jsonSchema = config.get('jsonSchema');
const output = config.get('resolve:output');
const verbose = config.get('quiet') ? 0 : config.get('verbose', 1);
const internalRefs = config.get('resolve:internalRefs');
const spec = await loader.readOrError(
file,
buildLoaderOptions(jsonSchema, verbose, internalRefs)
);
const content = yaml.stringify(spec);
return new Promise((resolve, reject) => {
if (output) {
fs.writeFile(output, content, 'utf8', err => {
if (err) {
if (verbose) console.error('Failed to write file: ' + err.message);
reject();
} else {
if (verbose) console.error('Resolved to ' + output);
resolve();
}
});
} else {
process.stdout.write(content, () => {
// Do not exit until the output is flushed (e.g. pipes)
resolve();
});
}
});
};
const buildLoaderOptions = (jsonSchema, verbose, internalRefs) => {
const options = {
resolve: true,
cache: [],
externals: [],
externalRefs: {},
rewriteRefs: true,
status: 'undefined',
filters: [],
verbose,
};
if (jsonSchema) options.filters.push(fromJsonSchema);
if (internalRefs) options.resolveInternal = true;
return options;
}
module.exports = { command };