-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathgenerate-command-data.js
More file actions
56 lines (47 loc) · 1.64 KB
/
generate-command-data.js
File metadata and controls
56 lines (47 loc) · 1.64 KB
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
import { createMainCommand } from '../../../src/commands/index.js'
import { compareOptions } from '../../../src/utils/command-helpers.js'
const program = createMainCommand()
const commands = [...program.commands].sort((cmdA, cmdB) => cmdA.name().localeCompare(cmdB.name()))
/**
*
* @param {import('../../../src/commands/base-command.js').default} command
*/
const parseCommand = function (command) {
const args = command._args.map(({ _name: name, description }) => ({
name,
description,
}))
const flags = command.options
.filter((option) => !option.hidden)
.sort(compareOptions)
.reduce((prev, cur) => {
const name = cur.long.replace('--', '')
const contentType = cur.argChoices ? cur.argChoices.join(' | ') : 'string'
return {
...prev,
[name]: {
description: cur.description,
char: cur.short,
type: cur.flags.includes('<') || cur.flags.includes('[') ? contentType : 'boolean',
},
}
}, {})
return {
name: command.name(),
description: command.description(),
commands: commands
.filter((cmd) => cmd.name().startsWith(`${command.name()}:`) && !cmd._hidden)
.map((cmd) => parseCommand(cmd)),
examples: command.examples.length !== 0 && command.examples,
args: args.length !== 0 && args,
flags: Object.keys(flags).length !== 0 && flags,
}
}
export const generateCommandData = function () {
return (
commands
// filter out sub commands
.filter((command) => !command.name().includes(':') && !command._hidden)
.reduce((prev, command) => ({ ...prev, [command.name()]: parseCommand(command) }), {})
)
}