-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
openapi-filter.js
executable file
·102 lines (91 loc) · 3.04 KB
/
openapi-filter.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const yaml = require('yaml');
const openapiFilter = require('./index.js');
let argv = require('yargs')
.usage('$0 <infile> [outfile]')
.demand(1)
.strict()
.boolean('info')
.describe('info','include complete info object with --valid')
.boolean('inverse')
.describe('inverse','output filtered elements only')
.alias('inverse','i')
.boolean('servers')
.describe('servers','include complete servers object with --valid')
.array('flags')
.alias('flags','f')
.describe('flags','flags to filter by')
.default('flags',['x-internal'])
.array('flagValues')
.alias('flagValues', 'v')
.describe('flagValues', 'flag string values to consider as a filter match (in addition to matching on boolean types)')
.default('flagValues', [])
.boolean('checkTags')
.describe('checkTags', 'filter if flags given in --flags are in the tags array')
.array('overrides')
.alias('overrides','o')
.default('overrides', [])
.describe('overrides', 'prefixes used to override named properties')
.boolean('valid')
.describe('valid', 'try to ensure inverse output is valid')
.boolean('strip')
.alias('strip','s')
.describe('strip','strip the flags from the finished product')
.number('lineWidth')
.alias('lineWidth','l')
.describe('lineWidth','max line width of yaml output')
.default('lineWidth',0)
.number('maxAliasCount')
.default('maxAliasCount',100)
.describe('maxAliasCount','maximum YAML aliases allowed')
.alias('configFile', 'c')
.describe('configFile', 'The file & path for the filter options')
.count('verbose')
.describe('verbose', 'Output more details of the filter process.')
.help()
.version()
.argv;
// Helper function to display info message, depending on the verbose level
function info(msg) {
if (argv.verbose >= 1) {
console.warn(msg);
}
}
info('=== Document filtering started ===\n')
// apply options from config file if present
if (argv && argv.configFile) {
info('Config File: ' + argv.configFile)
try {
let configFileOptions = {}
if (argv.configFile.indexOf('.json')>=0) {
configFileOptions = JSON.parse(fs.readFileSync(argv.configFile, 'utf8'));
} else {
configFileOptions = yaml.parse(fs.readFileSync(argv.configFile, 'utf8'), {schema:'core'});
}
argv = Object.assign({}, argv, configFileOptions);
} catch (err) {
console.error(err)
}
}
const infile = argv._[0];
const outfile = argv._[1];
info('Input file: ' + infile)
let s = fs.readFileSync(infile,'utf8');
let obj = yaml.parse(s, {maxAliasCount: argv.maxAliasCount});
let res = openapiFilter.filter(obj,argv);
if (infile.indexOf('.json')>=0) {
s = JSON.stringify(res,null,2);
}
else {
s = yaml.stringify(res, {lineWidth: argv.lineWidth});
}
if (outfile) {
fs.writeFileSync(outfile,s,'utf8');
info('Output file: ' + outfile)
}
else {
console.log(s);
}
info('\n✅ Document was filtered successfully')