forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widdershins.js
executable file
·182 lines (169 loc) · 5.67 KB
/
widdershins.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
const yaml = require('js-yaml');
const fetch = require('node-fetch');
const converter = require('./index.js');
var argv = require('yargs')
.usage('widdershins [options] {input-file|url} [[-o] output markdown]')
.demand(1)
.strict()
.boolean('code')
.alias('c','code')
.describe('code','Turn generic code samples off')
.string('customApiKeyValue')
.describe('customApiKeyValue','Set a custom API key value')
.string('includes')
.boolean('discovery')
.alias('d','discovery')
.describe('discovery','Include schema.org WebAPI discovery data')
.string('environment')
.alias('e','environment')
.describe('environment','Load config/override options from file')
.boolean('expandBody')
.describe('expandBody','Expand requestBody properties in parameters')
.number('headings')
.describe('headings','Levels of headings to expand in TOC')
.default('headings',2)
.boolean('httpsnippet')
.default('httpsnippet',false)
.describe('httpsnippet','Use httpsnippet to generate code samples')
.alias('i','includes')
.describe('includes','List of files to include, comma separated')
.boolean('lang')
.alias('l','lang')
.describe('lang','Automatically generate list of languages for code samples')
.array('language_tabs')
.describe('language_tabs', 'List of language tabs for code samples using "language[:label[:client]]" format, for example: "javascript:JavaScript:request"')
.number('maxLevel')
.alias('m','maxDepth')
.describe('maxDepth','Maximum depth for schema examples')
.default('maxDepth',10)
.boolean('omitBody')
.describe('omitBody','Omit top-level fake body parameter object')
.string('outfile')
.alias('o','outfile')
.describe('outfile','File to write output markdown to')
.boolean('raw')
.alias('r','raw')
.describe('raw','Output raw schemas not example values')
.boolean('resolve')
.describe('resolve','Resolve external $refs')
.boolean('search')
.alias('s','search')
.default('search',true)
.describe('search','Whether to enable search or not, default true')
.boolean('shallowSchemas')
.describe('shallowSchemas',"Don't expand schemas past $refs")
.boolean('summary')
.describe('summary','Use summary instead of operationId for TOC')
.string('theme')
.alias('t','theme')
.describe('theme','Syntax-highlighter theme to use')
.string('user_templates')
.alias('u','user_templates')
.describe('user_templates','directory to load override templates from')
.boolean('verbose')
.alias('v','verbose')
.describe('verbose','Increase verbosity')
.boolean('experimental')
.alias('x','experimental')
.describe('experimental','For backwards compatibility only, ignored')
.boolean('yaml')
.alias('y','yaml')
.describe('yaml','Display JSON schemas in YAML format')
.help('h')
.alias('h','help')
.version()
.argv;
var options = {};
function doit(s) {
var api = {};
try {
api = yaml.safeLoad(s,{json:true});
}
catch(ex) {
console.error('Failed to parse YAML/JSON, falling back to API Blueprint');
console.error(ex.message);
api = s;
}
converter.convert(api,options,function(err,output){
if (err) {
console.warn(err);
}
else {
var outfile = argv.outfile||argv._[1];
if (outfile) {
fs.writeFileSync(path.resolve(outfile),output,'utf8');
}
else {
console.log(output);
}
}
});
}
options.codeSamples = !argv.code;
options.httpsnippet = argv.httpsnippet;
if (argv.lang) {
options.language_tabs = [];
}
else if (argv.language_tabs) {
const languages = argv.language_tabs
.reduce((languages, item) => {
const [lang, name, client] = item.split(':', 3);
languages.language_tabs.push({ [lang]: name || lang });
languages.language_clients[lang] = client || '';
return languages;
}, { language_tabs: [], language_clients: []});
options.language_tabs = languages.language_tabs;
options.language_clients = languages.language_clients;
}
if (argv.theme) options.theme = argv.theme;
options.user_templates = argv.user_templates;
options.inline = argv.inline;
options.sample = !argv.raw;
options.discovery = argv.discovery;
options.verbose = argv.verbose;
if (options.verbose) Error.stackTraceLimit = Infinity;
options.tocSummary = argv.summary;
options.headings = argv.headings;
options.experimental = argv.experimental;
options.resolve = argv.resolve;
options.expandBody = argv.expandBody;
options.maxDepth = argv.maxDepth;
options.omitBody = argv.omitBody;
options.shallowSchemas = argv.shallowSchemas;
options.yaml = argv.yaml;
options.customApiKeyValue = argv.customApiKeyValue;
if (argv.search === false) options.search = false;
if (argv.includes) options.includes = argv.includes.split(',');
if (argv.environment) {
var e = fs.readFileSync(path.resolve(argv.environment),'utf8');
var env = {};
try {
env = yaml.safeLoad(e,{json:true});
}
catch (ex) {
console.error(ex.message);
}
options = Object.assign({},options,env);
}
var input = argv._[0];
options.source = input;
var up = url.parse(input);
if (up.protocol && up.protocol.startsWith('http')) {
fetch(input)
.then(function (res) {
return res.text();
}).then(function (body) {
doit(body);
}).catch(function (err) {
console.error(err.message);
});
}
else {
let s = fs.readFileSync(input,'utf8');
doit(s);
}