forked from Mermade/widdershins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widdershins.js
146 lines (135 loc) · 4.23 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
#!/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('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)
.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')
.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('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')
.describe('verbose','Increase verbosity')
.boolean('experimental')
.alias('x','experimental')
.describe('experimental','For backwards compatibility only, ignored')
.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){
var outfile = argv.outfile||argv._[1];
if (outfile) {
fs.writeFileSync(path.resolve(outfile),output,'utf8');
}
else {
console.log(output);
}
});
}
options.codeSamples = !argv.code;
if (argv.lang) {
options.language_tabs = [];
}
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;
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;
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];
var up = url.parse(input);
if (up.protocol && up.protocol.startsWith('http')) {
options.source = input;
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);
}