-
Notifications
You must be signed in to change notification settings - Fork 17
/
generate-cli-md.js
65 lines (54 loc) · 1.42 KB
/
generate-cli-md.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
import { getCommandMetaData } from './src/cli-metadata.js'
const formatHeader = ({ command, describe }) => {
return `
### calibre ${command}
${describe || ''}
`
}
const formatOptions = options => {
const optionKeys = Object.keys(options)
if (!optionKeys.length) {
return ''
} else {
return `Flags:
${optionKeys.map(key => {
// eslint-disable-next-line security/detect-object-injection
const {
describe,
default: defaultValue,
defaultDescription,
type
// eslint-disable-next-line security/detect-object-injection
} = options[key]
return `
* \`--${key}\`: ${describe}${
defaultValue
? ` (${defaultDescription ? defaultDescription : 'default'}: \`${defaultValue}\`)`
: ''
}${type ? ` (${type})` : ''}`
})}`
}
}
const template = command => {
let options = []
// Print subcommands or options
if (command.subcommands && !command.subcommands.length) {
options.push(formatHeader(command))
options.push(formatOptions(command.options))
} else {
for (const subcommand of command.subcommands) {
const newCommand = command.command.replace(
'<command>',
subcommand.command
)
options.push(template({ ...subcommand, command: newCommand }))
}
}
return [...options, null].join('\n')
}
const commands = getCommandMetaData()
let output = ''
commands.forEach(command => {
output += template(command)
})
console.log(output)