This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathgenerate_docs.js
More file actions
61 lines (53 loc) · 1.67 KB
/
generate_docs.js
File metadata and controls
61 lines (53 loc) · 1.67 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
57
58
59
60
61
/**
* Opted to use pure JS versus adding handlebars as a dep, as handlebars always has vulns..
*/
import process from 'node:process';
import _ from 'lodash';
import esMain from 'es-main';
function docObject(program) {
return program.commands.map((c) => ({
name: c._name,
description: c._description,
usage: c.usage(),
options: c.options.map((o) => ({
flags: o.flags,
defaultValue: o.defaultValue,
description: o.description,
})),
}));
}
// replace /n/t with HTML equivs
const cleanString = (s) => s
.replace(/\n/g, '')
.replace(/\t/g, '')
.replace(/</g, '`<')
.replace(/>/g, '>`');
// print sub-command flags as string
const optionRow = (opts) => _.join(
_.sortBy(opts, ['flags']).map(
(opt) => `\`${opt.flags}\`${_.isEmpty(opt.defaultValue) ? '' : ` (*default:* \`${opt.defaultValue}\`)`} - ${cleanString(opt.description)}`,
),
'<br />',
);
const subcmdTable = (subcmd) => `| Command | Description | Options |
| ------- | ----------- | ------- |\n${
_.join(
_.sortBy(subcmd, ['name']).map((cmd) => `| \`${cmd.name} ${cmd.usage}\` | ${cmd.description} | ${optionRow(cmd.options)} |`),
'\n',
)}`;
const cmdHeading = (cmd) => `
## ${_.capitalize(cmd.name())}
\`\`\`
${cmd.name()} ${cmd.usage()}
\`\`\`
${cmd.description()}
`;
export default function markdownForCmd(program) {
const docObj = docObject(program);
return `${cmdHeading(program)}\n${subcmdTable(docObj)}`;
}
if (esMain(import.meta)) {
const { default: program } = await import(process.argv[2]);
console.log(markdownForCmd(program));
}
// eslint-disable-next-line import/no-dynamic-require