-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
executable file
·105 lines (92 loc) · 2.65 KB
/
index.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
#!/usr/bin/env node
'use strict'
const fs = require('fs-extra')
const yaml = require('js-yaml')
const print = require('chalk-printer')
const command = require('meow')
const columnify = require('columnify')
const xmlComment = require('xml-comment-api')
// Command line definition.
const cli = command(`
Usage
$ markdown-swagger <swagger.yaml> <markdown.md>
Examples
$ markdown-swagger ./api/swagger/swagger.yaml ./README.md
`)
const [source, target] = cli.input
Promise.resolve()
.then(() => source || Promise.reject('The swagger source file is required.'))
.then(() => target || Promise.reject('The markdown target file is required.'))
.then(() => readSwaggerFile(source))
.then((swagger) => generateTable(swagger))
.then((table) => updateMarkdownTable(table, target))
.catch((error) => {
print.error(error)
process.exit(1)
})
function readSwaggerFile(source) {
return fs.readFile(source, 'utf-8')
.then((data) => yaml.safeLoad(data))
.catch((error) => {
if (error.code === 'ENOENT') {
throw new Error(`${source} doesn't exist`)
}
throw error;
})
}
const methods = ['get', 'post', 'put', 'delete', 'patch', 'options']
const headings = {
endpoint: 'Endpoint',
method: 'Method',
auth: 'Auth?',
description: 'Description'
}
function generateTable(yaml) {
const paths = yaml.paths || []
const data = []
Object.keys(paths).forEach((path) => {
const endpoint = paths[path]
const definedMethods = find(Object.keys(endpoint), methods)
definedMethods.forEach((method) => {
const security = endpoint[method].security
const description = endpoint[method].description
data.push({
endpoint: `\`${path}\``,
method: method.toUpperCase(),
auth: security ? 'Yes' : 'No',
description,
})
})
})
const table = columnify(data, {
columnSplitter: ' | ',
headingTransform: (data) => headings[data],
})
const rows = table.split('\n')
const divider = rows[0].replace(/[^\|]/g, '-').replace(/-\|-/g, ' | ')
rows.splice(1, 0, divider)
return rows.map((row) => ` ${row}`).join('\n')
}
function find(a = [], b = []) {
let found = []
a.find((ea) => {
const ib = b.indexOf(ea)
if (ib > -1) {
found.push(b[ib])
}
})
return found
}
function updateMarkdownTable(table, target) {
return fs.readFile(target, 'utf-8')
.then((data) => {
const updated = xmlComment(data).replace('markdown-swagger', `\n${table}\n`).contents()
return fs.writeFile(target, updated)
})
.catch((error) => {
if (error.code === 'ENOENT') {
throw new Error(`${source} doesn't exist`)
}
throw error;
})
}