-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
boast.js
executable file
·190 lines (179 loc) · 5.99 KB
/
boast.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env node
// this tool requires async/await, and therefore Node.js 8.3+
'use strict';
const fs = require('fs');
const http = require('http');
const https = require('https');
const yaml = require('yaml');
const fetch = require('node-fetch');
const swagger2openapi = require('./index.js');
const validator = require('oas-validator');
const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });
const bobwAgent = function(_parsedURL) {
if (_parsedURL.protocol == 'http:') {
return httpAgent;
} else {
return httpsAgent;
}
};
process.exitCode = 1;
let argv = require('yargs')
.boolean('anchors')
.describe('anchors','allow use of YAML anchors/aliases')
.boolean('all')
.alias('a','all')
.describe('all','show all lint warnings')
.string('encoding')
.alias('e','encoding')
.default('encoding','utf8')
.describe('encoding','text encoding to use')
.boolean('force')
.alias('f','force')
.describe('force','output even if validation/lint failures')
.boolean('internal')
.alias('i','internal')
.describe('internal','resolve internal $refs also')
.boolean('json')
.alias('j','json')
.describe('json','output validation/lint errors in JSON format')
.boolean('lint')
.describe('lint','also lint the document')
.alias('l','lint')
.array('lintSkip')
.describe('lintSkip','linter rule name(s) to skip')
.alias('s','lintSkip')
.boolean('dumpMeta')
.alias('m','dumpMeta')
.describe('dumpMeta','Dump definition metadata')
.boolean('repair')
.alias('r','repair')
.boolean('nocert')
.alias('n','nocert')
.describe('nocert','Do not check server certificates')
.string('output')
.alias('o','output')
.describe('output','outfile file to write to, default STDOUT')
.boolean('prevalidate')
.alias('p','prevalidate')
.describe('prevalidate','validate $ref\'d files separately')
.count('quiet')
.alias('q','quiet')
.describe('quiet','reduce verbosity')
.count('verbose')
.default('verbose',1)
.alias('v','verbose')
.describe('verbose','increase verbosity')
.demand(1)
.argv;
function main(){
return new Promise(async function(resolve,reject){
const ruleUrls = new Set();
argv.resolve = true;
argv.patch = true;
argv.source = argv._[0];
argv.fatal = true;
argv.laxurls = true;
argv.laxDefaults = true;
argv.fetch = fetch;
argv.agent = bobwAgent;
if (argv.all) argv.lintLimit = Number.MAX_SAFE_INTEGER;
if (argv.internal) {
argv.resolveInternal = true;
}
argv.verbose = argv.verbose - argv.quiet;
if (argv.nocert) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
httpsAgent.options.rejectUnauthorized = false;
}
let options = {};
let result = false;
let jsonOutput = {};
try {
if (argv.source.startsWith('http')) {
options = await swagger2openapi.convertUrl(argv.source,argv);
}
else {
options = await swagger2openapi.convertFile(argv.source,argv);
}
result = await validator.validateInner(options.openapi,options);
}
catch (ex) {
let path;
if (options.context) {
path = options.context.pop();
}
if (options.json) {
jsonOutput.error = ex.message;
if (options.verbose > 1) jsonOutput.stacktrace = ex.stack;
if (path) {
jsonOutput.path = path;
}
}
else {
console.warn(ex.message);
if (options.verbose > 1) console.warn(ex.stack);
if (path) {
console.warn(path);
}
}
jsonOutput.warnings = [];
if (options.warnings) {
for (let warning of options.warnings) {
if (options.json) {
jsonOutput.warnings.push({ message:warning.message, pointer:warning.pointer, ruleName:warning.ruleName, ruleUrl:warning.rule.url });
}
else {
console.warn(warning.message,warning.pointer,warning.ruleName);
if (warning.rule.url) ruleUrls.add(warning.rule.url+'#'+warning.ruleName);
}
}
}
reject(ex);
}
if ((ruleUrls.size > 0) && (!options.json)) {
console.warn('For more information, visit:');
for (let url of ruleUrls) {
console.warn(url);
}
}
if (argv.dumpMeta) {
if (options.json) {
jsonOutput.metadata = options.metadata;
}
else {
console.warn('\n#Definition metadata:');
console.warn(yaml.stringify(options.metadata,{depth:Math.INFINITY}));
}
}
if (options.json) {
console.warn(JSON.stringify(jsonOutput, null, 2));
}
if (result || argv.force) {
if (options.output) {
if (options.sourceYaml) {
fs.writeFileSync(options.output, yaml.stringify(options.openapi),options.encoding);
}
else {
fs.writeFileSync(options.output, JSON.stringify(options.openapi,null,2),options.encoding);
}
}
else if (argv.verbose >= 1) {
if (options.sourceYaml) {
console.log(yaml.stringify(options.openapi));
}
else {
console.log(JSON.stringify(options.openapi,null,2));
}
}
}
resolve(options.openapi);
});
}
main()
.then(function(options){
process.exitCode = 0;
})
.catch(function(err){
//if (!argv.json) console.warn(err.message);
});