-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathopenapi-extract.js
More file actions
executable file
·93 lines (86 loc) · 2.66 KB
/
openapi-extract.js
File metadata and controls
executable file
·93 lines (86 loc) · 2.66 KB
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
#!/usr/bin/env node
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const yaml = require('yaml');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const extractor = require('./index.js');
let argv = require('yargs')
.usage('Usage: openapi-extract [options] {infile} [{outfile}]')
.demand(1)
.strict()
.help('h')
.alias('h', 'help')
.version()
.string('path')
.alias('p','path')
.describe('path','the path to extract')
.boolean('openai')
.describe('openai','make definition OpenAI compliant')
.array('operationid')
.alias('o','operationid')
.describe('operationid','the operationIds to extract')
.string('method')
.alias('m','method')
.describe('method','the method to extract for the given path')
.boolean('info')
.alias('i','info')
.describe('info','copy full info object, otherwise minimal')
.boolean('server')
.describe('server','include server information')
.boolean('security')
.alias('s','security')
.describe('security','include security information')
.boolean('removeDocs')
.alias('d','removeDocs')
.describe('removeDocs','remove all externalDocs properties')
.boolean('removeExamples')
.alias('r','removeExamples')
.describe('removeExamples','remove all example/examples properties')
.boolean('removeExtensions')
.alias('x','removeExtensions')
.describe('removeExtensions','remove all x- extension properties')
.string('shard')
.describe('shard','shard the input to an output directory')
.boolean('verbose')
.alias('v','verbose')
.describe('verbose','increase verbosity')
.argv;
async function main() {
let s = fs.readFileSync(argv._[0],'utf8');
let obj = yaml.parse(s);
let filename = 'openapi.yaml';
if (obj.asyncapi) filename = 'asyncapi.yaml';
if (argv.shard) {
const map = extractor.shard(obj, argv);
await rimraf(argv.shard, { preserveRoot: false });
for (let [key, value] of map) {
process.stderr.write('.');
try {
await mkdirp(path.resolve(argv.shard,key));
fs.writeFileSync(path.resolve(argv.shard,key,filename),yaml.stringify(value),'utf8');
}
catch (ex) {
process.stderr.write(`\n${ex.message}\n`);
}
}
process.stderr.write('\n');
}
else {
let res = extractor.extract(obj, argv);
if (argv._[0].indexOf('.json')>=0) {
s = JSON.stringify(res,null,2);
}
else {
s = yaml.stringify(res);
}
if (argv._.length>1) {
fs.writeFileSync(argv._[1],s,'utf8');
}
else {
console.log(s);
}
}
}
main();