-
Notifications
You must be signed in to change notification settings - Fork 18
/
lib.js
executable file
·69 lines (53 loc) · 2.06 KB
/
lib.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
const fs = require('fs')
const path = require('path')
const apidoc = require('apidoc-core')
const winston = require('winston');
const apidoc_to_swagger = require('./apidoc_to_swagger');
apidoc.setGeneratorInfos({ name: 'name', time: new Date(), version: '0.0.1', url: 'xxx url' })
function generateLog(options) {
return winston.createLogger({
transports: [
new (winston.transports.Console)({
level: options.verbose ? 'verbose' : 'info',
silent: false,
prettyPrint: true,
colorize: options.color,
timestamp: false
}),
]
})
}
function main(options) {
options.verbose && console.log('options', options);
const log = generateLog(options)
const { src, dest, verbose } = options
apidoc.setLogger(log)
var api = apidoc.parse({ ...options, log: log })
if (!api) {
console.log('No input data found, check your include/exclude filters');
return
}
var apidocData = JSON.parse(api.data);
var projectData = JSON.parse(api.project);
// Replicate underscoreToSpace handlebar filter from https://github.com/apidoc/apidoc/blob/0.50.5/template/src/hb_helpers.js#L93
for (let article of apidocData) {
if (article.name)
article.name = article.name.replace(/(_+)/g, ' ');
}
const swagger = apidoc_to_swagger.toSwagger(apidocData, projectData)
api["swaggerData"] = JSON.stringify(swagger, null, 4);
createOutputFile(api.swaggerData, log, options)
return swagger;
}
function createOutputFile(swaggerData, log, options) {
if (options.simulate)
log.warn('!!! Simulation !!! No file or dir will be copied or created.');
log.verbose('create dir: ' + options.dest);
if (!options.simulate)
fs.existsSync(options.dest) || fs.mkdirSync(options.dest);
//Write swagger
log.verbose('write swagger json file: ' + options.dest + 'swagger.json');
if (!options.simulate)
fs.writeFileSync(options.dest + './swagger.json', swaggerData);
}
exports.main = main