1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | const fs = require('fs');
|
7 | const url = require('url');
|
8 |
|
9 | const yaml = require('yaml');
|
10 | const converter = require('./index.js');
|
11 |
|
12 |
|
13 | let argv = require('yargs')
|
14 | .boolean('anchors')
|
15 | .describe('anchors','allow use of YAML anchors/aliases')
|
16 | .boolean('components')
|
17 | .alias('c', 'components')
|
18 | .describe('components', 'output information to unresolve a definition')
|
19 | .boolean('debug')
|
20 | .alias('d', 'debug')
|
21 | .describe('debug', 'enable debug mode, adds specification-extensions')
|
22 | .string('encoding')
|
23 | .alias('e', 'encoding')
|
24 | .default('encoding', 'utf8')
|
25 | .describe('encoding', 'encoding for input/output files')
|
26 | .boolean('fatal')
|
27 | .alias('f','fatal')
|
28 | .describe('fatal','make resolution errors fatal')
|
29 | .help('help')
|
30 | .alias('h', 'help')
|
31 | .string('indent')
|
32 | .alias('i','indent')
|
33 | .describe('indent','JSON indent to use, defaults to 4 spaces')
|
34 | .string('outfile')
|
35 | .alias('o', 'outfile')
|
36 | .describe('outfile', 'the output file to write to')
|
37 | .boolean('patch')
|
38 | .alias('p', 'patch')
|
39 | .describe('patch', 'fix up small errors in the source definition')
|
40 | .choices('refSiblings',['remove','preserve','allOf'])
|
41 | .describe('refSiblings','mode to handle $ref\'s with sibling properties')
|
42 | .boolean('resolve')
|
43 | .alias('r', 'resolve')
|
44 | .describe('resolve', 'resolve external references')
|
45 | .boolean('resolveInternal')
|
46 | .describe('resolveInternal', 'resolve internal references also')
|
47 | .string('targetVersion')
|
48 | .alias('t','targetVersion')
|
49 | .describe('targetVersion','override default target version of 3.0.0')
|
50 | .string('url')
|
51 | .describe('url', 'url of original spec, creates x-origin entry')
|
52 | .alias('u', 'url')
|
53 | .count('verbose')
|
54 | .alias('v', 'verbose')
|
55 | .describe('verbose', 'increase verbosity')
|
56 | .boolean('warnOnly')
|
57 | .alias('w','warnOnly')
|
58 | .describe('warnOnly','Do not throw on non-patchable errors, add warning extensions')
|
59 | .string('warnProperty')
|
60 | .describe('warnProperty','Property name to use for warning extensions')
|
61 | .default('warnProperty','x-s2o-warning')
|
62 | .boolean('yaml')
|
63 | .alias('y', 'yaml')
|
64 | .describe('yaml', 'write YAML, default JSON (overridden by --outfile filepath extension)')
|
65 | .string('rbname')
|
66 | .alias('b', 'rbname')
|
67 | .default('rbname', '')
|
68 | .describe('rbname', 'Extension to use to preserve body parameter names in converted operations ("" == disabled)')
|
69 | .require(1)
|
70 | .strict()
|
71 | .version()
|
72 | .argv;
|
73 |
|
74 | function processResult(err, options) {
|
75 | if (err) {
|
76 | delete err.options;
|
77 | console.warn(err);
|
78 | return process.exitCode = 1;
|
79 | }
|
80 | if (options.yaml && options.outfile && options.outfile.indexOf('.json') > 0) {
|
81 | options.yaml = false;
|
82 | }
|
83 | if (!options.yaml && options.outfile && options.outfile.indexOf('.yaml') > 0) {
|
84 | options.yaml = true;
|
85 | }
|
86 |
|
87 | let s;
|
88 | try {
|
89 | if (options.yaml) {
|
90 | s = yaml.stringify(options.openapi);
|
91 | }
|
92 | else {
|
93 | s = JSON.stringify(options.openapi, null, options.indent||4);
|
94 | }
|
95 | }
|
96 | catch (ex) {
|
97 | console.warn('The result cannot be represented safely in the chosen output format');
|
98 | s = '{}';
|
99 | }
|
100 |
|
101 | if (argv.outfile) {
|
102 | fs.writeFileSync(options.outfile, s, options.encoding || 'utf8');
|
103 | }
|
104 | else {
|
105 | console.log(s);
|
106 | }
|
107 |
|
108 | if (argv.components) {
|
109 | console.warn(JSON.stringify(options.externals, null, options.indent||4));
|
110 | }
|
111 | }
|
112 |
|
113 | argv.source = argv._[0];
|
114 | argv.text = true;
|
115 | let u = url.parse(argv.source);
|
116 | if (u.protocol && u.protocol.startsWith('http')) {
|
117 | converter.convertUrl(argv.source, argv, processResult);
|
118 | }
|
119 | else {
|
120 | argv.origin = argv.url;
|
121 | converter.convertFile(argv.source, argv, processResult);
|
122 | }
|
123 |
|