-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcli.mjs
executable file
·139 lines (122 loc) · 4.13 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
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
137
138
139
#!/usr/bin/env node
import { resolve } from 'node:path';
import { argv, exit } 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';
import createLinter from '../src/linter/index.mjs';
import reporters from '../src/linter/reporters/index.mjs';
import rules from '../src/linter/rules/index.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(
'--ignore [patterns...]',
'Specify which input files to ignore using glob syntax'
)
)
.addOption(
new Option(
'-o, --output <path>',
'Specify the relative or absolute output directory'
)
)
.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)
)
.addOption(
new Option('--disable-rule [rule...]', 'Disable a specific linter rule')
.choices(Object.keys(rules))
.default([])
)
.addOption(
new Option('--lint-dry-run', 'Run linter in dry-run mode').default(false)
)
.addOption(
new Option('-r, --reporter [reporter]', 'Specify the linter reporter')
.choices(Object.keys(reporters))
.default('console')
)
.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.
* @property {string[]} disableRule Specifies the linter rules to disable.
* @property {boolean} lintDryRun Specifies whether the linter should run in dry-run mode.
* @property {keyof reporters} reporter Specifies the linter reporter.
*
* @name ProgramOptions
* @type {Options}
* @description The return type for values sent to the program from the CLI.
*/
const {
input,
ignore,
output,
target = [],
version,
changelog,
disableRule,
lintDryRun,
reporter,
} = program.opts();
const linter = createLinter(lintDryRun, disableRule);
const { loadFiles } = createMarkdownLoader();
const { parseApiDocs } = createMarkdownParser(linter);
const apiDocFiles = await loadFiles(input, ignore);
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);
linter.lintAll(parsedApiDocs);
if (target && output) {
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(),
});
}
linter.report(reporter);
exit(Number(linter.hasError()));