-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen-docs.js
136 lines (120 loc) · 3.67 KB
/
gen-docs.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
#!/usr/bin/env node
// usage: node gen-docs.js -c ./conf.json --no-cache
'use strict'
const tool = require('command-line-tool')
const fastGlob = require('fast-glob')
const path = require('path')
const version = "1.0"
const cli = parseCommandLine()
let options = cli.options._all
options = loadStoredConfig(options)
/* jsdoc2md --help */
if (options.help) {
tool.printOutput(cli.usage)
/* jsdoc2md --version */
} else if (options.version) {
tool.printOutput(version)
/* jsdoc2md --clear */
} else if (options.clear) {
const jsdoc2md = require('../')
jsdoc2md._interface = 'cli'
jsdoc2md.clear().catch(handleError)
} else {
const jsdoc2md = require('jsdoc-to-markdown')
jsdoc2md._interface = 'cli'
/* jsdoc2md --config */
if (options.config) {
const omit = require('lodash.omit')
tool.stop(JSON.stringify(omit(options, 'config'), null, ' '))
}
/* input files (jsdoc-options) required from here */
/* input validation */
/*try {
const assert = require('assert')
options.files = options.files || []
assert.ok(options.files.length || options.source, 'Must supply either --files or --source')
} catch (err) {
tool.printOutput(cli.usage)
handleError(err)
}*/
/* jsdoc2md --json */
if (options.json) {
jsdoc2md.getTemplateData(options)
.then(function(json) {
tool.printOutput(JSON.stringify(json, null, ' '))
})
.catch(handleError)
/* jsdoc2md --jsdoc */
} else if (options.jsdoc) {
jsdoc2md
.getJsdocData(options)
.then(function(json) {
tool.printOutput(JSON.stringify(json, null, ' '))
})
.catch(handleError)
/* jsdoc2md --namepaths */
} else if (options.namepaths) {
jsdoc2md
.getNamepaths(options)
.then(function(namepaths) {
tool.printOutput(JSON.stringify(namepaths, null, ' '))
})
.catch(handleError)
/* jsdoc2md [<options>] --src <files> */
} else {
const fs = require('fs')
if (options.template) options.template = fs.readFileSync(options.template, 'utf8')
let files = fastGlob.sync('lib/*.js')
for (let file of files) {
console.log(file)
options.files = file
jsdoc2md
.render(options)
.then(output => {
console.log('writing: ' + 'docs/'+path.basename(file).replace('.js',".md"))
fs.writeFile('docs/'+path.basename(file).replace('.js',".md"), output)
})
.catch(handleError)
}
files = fastGlob.sync('lib/services/*.js')
for (let file of files) {
console.log(file)
options.files = file
jsdoc2md
.render(options)
.then(output => {
console.log('writing: ' + 'docs/'+path.basename(file).replace('.js',".md"))
fs.writeFile('docs/services/'+path.basename(file).replace('.js',".md"), output)
})
.catch(handleError)
}
files = fastGlob.sync('lib/models/*.js')
for (let file of files) {
console.log(file)
options.files = file
jsdoc2md
.render(options)
.then(output => {
console.log('writing: ' + 'docs/'+path.basename(file).replace('.js',".md"))
fs.writeFile('docs/'+path.basename(file).replace('.js',".md"), output)
})
.catch(handleError)
}
}
}
function loadStoredConfig(options) {
const loadConfig = require('config-master')
const jsdoc2mdConfig = loadConfig('jsdoc2md')
return Object.assign(jsdoc2mdConfig, options)
}
function parseCommandLine() {
const cliData = require('./node_modules/jsdoc-to-markdown/lib/cli-data')
try {
return tool.getCli(cliData.definitions, cliData.usageSections)
} catch (err) {
handleError(err)
}
}
function handleError(err) {
tool.halt(err.toString())
}