forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 5
/
widdershins.js
executable file
·245 lines (231 loc) · 8.87 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
const yaml = require('yaml');
const fetch = require('node-fetch');
const converter = require('./lib/index.js');
var argv = require('yargs')
.usage('widdershins [options] {input-file|url} [[-o] output markdown]')
.demand(1)
.strict()
.string('abstract')
.alias('a','abstract')
.describe('abstract','The filename of the Markdown include file to use for the ReSpec abstract section.')
.default('abstract','./include/abstract.md')
.boolean('code')
.alias('c','code')
.describe('code','Omit generated code samples.')
.string('customApiKeyValue')
.describe('customApiKeyValue','Set a custom API key value to use as the API key in generated code examples.')
.string('includes')
.boolean('discovery')
.alias('d','discovery')
.describe('discovery','Include schema.org WebAPI discovery data.')
.string('environment')
.alias('e','environment')
.describe('environment','File to load config options from.')
.boolean('expandBody')
.describe('expandBody','Expand the schema and show all properties in the request body.')
.number('headings')
.describe('headings','Set the value of the `headingLevel` parameter in the header so Shins knows how many heading levels to show in the table of contents.')
.default('headings',2)
.boolean('httpsnippet')
.default('httpsnippet',false)
.describe('httpsnippet','Use httpsnippet to generate code samples.')
.boolean('html')
.describe('html','Output html instead of Markdown; implies omitHeader.')
.alias('i','includes')
.describe('includes','List of files to put in the `include` header of the output Markdown.')
.boolean('lang')
.alias('l','lang')
.describe('lang','Generate the list of languages for code samples based on the languages used in the source file\'s `x-code-samples` examples.')
.array('language_tabs')
.describe('language_tabs', 'List of language tabs for code samples using "language[:label[:client]]" format, such as `javascript:JavaScript:request`.')
.number('maxLevel')
.alias('m','maxDepth')
.describe('maxDepth','Maximum depth to show for schema examples.')
.default('maxDepth',10)
.boolean('omitBody')
.describe('omitBody','Omit the body parameter from the parameters table.')
.boolean('omitHeader')
.describe('omitHeader','Omit the header / YAML front-matter in the generated Markdown file.')
.string('outfile')
.alias('o','outfile')
.describe('outfile','File to write the output markdown to. If left blank, Widdershins sends the output to stdout.')
.boolean('raw')
.alias('r','raw')
.describe('raw','Output raw schemas instead of example values.')
.boolean('resolve')
.describe('resolve','Resolve external $refs')
.string('respec')
.describe('respec','Filename containing the ReSpec config object; implies html and omitHeader.')
.boolean('search')
.alias('s','search')
.default('search',true)
.describe('search','Set the value of the `search` parameter in the header so Markdown processors like Shins include search or not in their output.')
.boolean('shallowSchemas')
.describe('shallowSchemas',"When referring to a schema with a $ref, don't show the full contents of the schema.")
.string('sotd')
.describe('sotd','The filename of the markdown include file to use for the ReSpec SotD section.')
.default('sotd','./include/sotd.md')
.boolean('summary')
.describe('summary','Use the operation summary as the TOC entry instead of the ID.')
.string('theme')
.alias('t','theme')
.describe('theme','Syntax-highlighter theme to use.')
.boolean('useBodyName')
.describe('useBodyName','Use original param name for OpenAPI 2.0 body parameter')
.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.')
.string('splitOutPath')
.alias('split','splitOutPath')
.describe('splitOutPath','Will split the output in two parts and creates two files in core-resources/index.html.md and reference/index.html.md in the provided path')
.help('h')
.alias('h','Show help.')
.version()
.argv;
var options = {};
function doit(s) {
var api = {};
try {
api = yaml.parse(s);
}
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 {
let outfile = argv.outfile||argv._[1];
if (outfile) {
fs.writeFileSync(path.resolve(outfile),output,'utf8');
}
else if(options.splitOutPath) {
const sectionName = options.source.split('/')[4];
switch(sectionName) {
case "customers":
fs.writeFileSync(
path.resolve(`${options.splitOutPath}/customer-resources/index.html.md`),output[0],
'utf8'
);
fs.writeFileSync(
path.resolve(`${options.splitOutPath}/customer-reference/index.html.md`),output[1],
'utf8'
);
break;
case "hackers":
fs.writeFileSync(
path.resolve(`${options.splitOutPath}/hacker-resources/index.html.md`),output[0],
'utf8'
);
fs.writeFileSync(
path.resolve(`${options.splitOutPath}/hacker-reference/index.html.md`),output[1],
'utf8'
);
break;
default:
}
}
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.omitHeader = argv.omitHeader;
options.shallowSchemas = argv.shallowSchemas;
options.yaml = argv.yaml;
options.customApiKeyValue = argv.customApiKeyValue;
options.html = argv.html;
options.respec = argv.respec;
options.useBodyName = argv.useBodyName;
options.splitOutPath = argv.splitOutPath;
if (argv.search === false) options.search = false;
if (argv.includes) options.includes = argv.includes.split(',');
if (argv.respec) {
options.abstract = argv.abstract;
options.sotd = argv.sotd;
let r = fs.readFileSync(path.resolve(argv.respec),'utf8');
try {
options.respec = yaml.parse(r);
}
catch (ex) {
console.error(ex.message);
}
}
if (options.respec) options.html = true;
if (options.html) options.omitHeader = true;
if (argv.environment) {
var e = fs.readFileSync(path.resolve(argv.environment),'utf8');
var env = {};
try {
env = yaml.parse(e);
}
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);
}