forked from Mermade/oas-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswagger2openapi.js
executable file
·101 lines (91 loc) · 2.87 KB
/
swagger2openapi.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
// @ts-check
'use strict';
var fs = require('fs');
var url = require('url');
var util = require('util');
var yaml = require('js-yaml');
var converter = require('./index.js');
// @ts-ignore
var argv = require('yargs')
.boolean('components')
.alias('c', 'components')
.describe('components', 'output information to unresolve a definition')
.boolean('debug')
.alias('d', 'debug')
.describe('debug', 'enable debug mode, adds specification-extensions')
.string('encoding')
.alias('e', 'encoding')
.default('encoding', 'utf8')
.describe('encoding', 'encoding for input/output files')
.help('help')
.alias('h', 'help')
.string('indent')
.alias('i','indent')
.describe('indent','JSON indent to use, defaults to 4 spaces')
.string('outfile')
.alias('o', 'outfile')
.describe('outfile', 'the output file to write to')
.boolean('patch')
.alias('p', 'patch')
.describe('patch', 'fix up small errors in the source definition')
.boolean('resolve')
.alias('r', 'resolve')
.describe('resolve', 'resolve external references')
.string('url')
.describe('url', 'url of original spec, creates x-origin entry')
.alias('u', 'url')
.count('verbose')
.alias('v', 'verbose')
.describe('verbose', 'increase verbosity')
.boolean('warnOnly')
.alias('w','warnOnly')
.describe('warnOnly','Do not throw on non-patchable errors, add warning extensions')
.string('warnProperty')
.describe('warnProperty','Property name to use for warning extensions')
.default('warnProperty','x-s2o-warning')
.boolean('yaml')
.alias('y', 'yaml')
.describe('yaml', 'read and write YAML, default JSON')
.require(1)
.strict()
.version()
.argv;
function processResult(err, options) {
if (err) {
delete err.options;
console.log(util.inspect(err));
return process.exitCode = 1;
}
if (options.yaml && options.outfile && options.outfile.indexOf('.json') > 0) {
options.yaml = false;
}
if (!options.yaml && options.outfile && options.outfile.indexOf('.yaml') > 0) {
options.yaml = true;
}
var s;
if (options.yaml) {
s = options.debug ? yaml.dump(options.openapi) : yaml.safeDump(options.openapi);
}
else {
s = JSON.stringify(options.openapi, null, options.indent||4);
}
if (argv.outfile) {
fs.writeFileSync(options.outfile, s, options.encoding || 'utf8');
}
else {
console.log(s);
}
if (argv.components) {
console.log(JSON.stringify(options.externals, null, options.indent||4));
}
}
argv.source = argv._[0];
var u = url.parse(argv.source);
if (u.protocol && u.protocol.startsWith('http')) {
converter.convertUrl(argv.source, argv, processResult);
}
else {
argv.origin = argv.url;
converter.convertFile(argv.source, argv, processResult);
}