-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.mjs
executable file
·94 lines (81 loc) · 2.96 KB
/
cli.mjs
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
#!/usr/bin/env node
import { resolve } from 'node:path';
import { argv } from 'node:process';
import { Command, Option } from 'commander';
import { coerce } from 'semver';
import { DOC_NODE_CHANGELOG_URL, DOC_NODE_VERSION } from '../src/constants.mjs';
import createGenerator from '../src/generators.mjs';
import generators from '../src/generators/index.mjs';
import createMarkdownLoader from '../src/loaders/markdown.mjs';
import createMarkdownParser from '../src/parsers/markdown.mjs';
import createNodeReleases from '../src/releases.mjs';
const availableGenerators = Object.keys(generators);
const program = new Command();
program
.name('api-docs-tooling')
.description('CLI tool to generate API documentation of a Node.js project.')
.addOption(
new Option(
'-i, --input [patterns...]',
'Specify input file patterns using glob syntax'
).makeOptionMandatory()
)
.addOption(
new Option(
'-o, --output <path>',
'Specify the relative or absolute output directory'
).makeOptionMandatory()
)
.addOption(
new Option(
'-v, --version <semver>',
'Specify the target version of Node.js, semver compliant'
).default(DOC_NODE_VERSION)
)
.addOption(
new Option(
'-c, --changelog <url>',
'Specify the path (file: or https://) to the CHANGELOG.md file'
).default(DOC_NODE_CHANGELOG_URL)
)
.addOption(
new Option(
'-t, --target [mode...]',
'Set the processing target modes'
).choices(availableGenerators)
)
.parse(argv);
/**
* @typedef {keyof generators} Target A list of the available generator names.
*
* @typedef {Object} Options
* @property {Array<string>|string} input Specifies the glob/path for input files.
* @property {string} output Specifies the directory where output files will be saved.
* @property {Target[]} target Specifies the generator target mode.
* @property {string} version Specifies the target Node.js version.
* @property {string} changelog Specifies the path to the Node.js CHANGELOG.md file
*
* @name ProgramOptions
* @type {Options}
* @description The return type for values sent to the program from the CLI.
*/
const { input, output, target = [], version, changelog } = program.opts();
const { loadFiles } = createMarkdownLoader();
const { parseApiDocs } = createMarkdownParser();
const apiDocFiles = loadFiles(input);
const parsedApiDocs = await parseApiDocs(apiDocFiles);
const { runGenerators } = createGenerator(parsedApiDocs);
// Retrieves Node.js release metadata from a given Node.js version and CHANGELOG.md file
const { getAllMajors } = createNodeReleases(changelog);
await runGenerators({
// A list of target modes for the API docs parser
generators: target,
// Resolved `input` to be used
input: input,
// Resolved `output` path to be used
output: resolve(output),
// Resolved SemVer of current Node.js version
version: coerce(version),
// A list of all Node.js major versions with LTS status
releases: await getAllMajors(),
});