forked from Mermade/openapi_optimise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi_optimise.js
executable file
·133 lines (118 loc) · 3.3 KB
/
openapi_optimise.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
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var yaml = require('js-yaml');
var common = require('./common.js');
var opt = require('./index.js');
var deref = require('./schema_deref.js');
var circular = require('./circular.js');
var argv = require('yargs')
.usage('openapi_optimise [options] {source} [{target}]')
.count('verbose')
.alias('v','verbose')
.alias('n','nondefault')
.describe('nondefault','apply non-default operations')
.boolean('nondefault')
.describe('verbose','verbosity level, repeat for more logging')
.alias('u','unindent')
.boolean('unindent')
.describe('unindent','no indentation/linefeeds')
.boolean('analyse')
.alias('a','analyse')
.describe('analyse','analyse structure of specification')
.boolean('expand')
.alias('e','expand')
.describe('expand','expand all local $refs before any model compression')
.boolean('inline')
.alias('i','inline')
.describe('inline','inline $refs rather than moving to #/definitions')
.boolean('force')
.alias('f','force')
.describe('force','allow overwriting of source with target')
.boolean('jsyaml')
.alias('j','jsyaml')
.default('jsyaml',true)
.describe('jsyaml','use jsyaml for output')
.boolean('skip-defaults')
.alias('s','skip-defaults')
.describe('skip-defaults','do not perform default optimisations')
.boolean('preserve-tags')
.alias('t','preserve-tags')
.describe('preserve-tags','preserve tags with vendor extensions')
.boolean('yaml')
.alias('y','yaml')
.describe('yaml','read and write specification in yaml format (default JSON)')
.boolean('yamlread')
.alias('r','yamlread')
.describe('yamlread','read specification in yaml format')
.boolean('yamlwrite')
.alias('w','yamlwrite')
.describe('yamlwrite','write specification in yaml format')
.boolean('debug')
.alias('d','debug')
.describe('debug','debug options')
.demand(1)
.strict()
.help('h')
.alias('h', 'help')
.version()
.argv;
var logger = new common.logger(argv.verbose);
if (argv.analyse) {
if (argv.verbose<2) argv.verbose = 2;
argv.skipDefaults = true;
}
if (argv.debug) {
logger.write(JSON.stringify(argv,null,2));
process.exit();
}
var infile = argv._[0];
var outfile = (argv._.length>1 ? argv._[1] : '');
if ((infile === outfile) && (!argv.force)) {
logger.write('source and target are same, use --force if sure');
process.exit(1);
}
var src;
var infd;
// Read from STDIN if infile is literal '-'
if (infile === '-') {
infd = 0;
} else {
infd = path.resolve(infile);
}
var srcStr = fs.readFileSync(infd,'utf8');
src = yaml.safeLoad(srcStr,{json:true});
var dest;
if (argv.skipDefaults) {
//dest = _.cloneDeep(src);
dest = opt.minimumOptimisations(src,argv);
if (argv.analyse) {
var circles = circular.getCircularRefs(dest,argv);
console.log('Circular references %s',circles.length);
}
}
else {
dest = opt.defaultOptimisations(src,argv);
}
if ((argv.expand) && (!argv.nondefault)) {
dest = deref.expand(dest,argv);
}
if (argv.nondefault) {
dest = opt.nonDefaultOptimisations(dest,argv);
}
var outStr;
if ((argv.yaml) || (argv.yamlwrite)) {
outStr = yaml.safeDump(dest,{lineWidth:-1});
}
else {
var indent = (argv.unindent ? '' : '\t');
outStr = JSON.stringify(dest,null,indent);
}
if (outfile) {
fs.writeFileSync(outfile,outStr,'utf8');
}
else {
if (!argv.analyse) logger.write(outStr);
}