-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjsdoc-md.mjs
executable file
·90 lines (75 loc) · 2.38 KB
/
jsdoc-md.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
#!/usr/bin/env node
import arg from "arg";
import CliError from "./CliError.mjs";
import jsdocMd from "./jsdocMd.mjs";
import reportCliError from "./reportCliError.mjs";
/**
* Runs the `jsdoc-md` CLI.
* @kind function
* @name jsdocMdCli
* @returns {Promise<void>} Resolves once the operation is done.
* @ignore
*/
async function jsdocMdCli() {
try {
const { _: unexpectedArgs, ...expectedArgs } = arg(
{
"--source-glob": String,
"-s": "--source-glob",
"--markdown-path": String,
"-m": "--markdown-path",
"--target-heading": String,
"-t": "--target-heading",
"--check": Boolean,
"-c": "--check",
},
{
// Don’t throw on unexpected arguments.
permissive: true,
}
);
if (unexpectedArgs.length)
throw new CliError(
`Command \`jsdoc-md\` unexpected argument${
unexpectedArgs.length === 1 ? "" : "s"
} \`${unexpectedArgs.join("`, `")}\`.`
);
for (const [argName, argValue] of Object.entries(expectedArgs))
if (typeof argValue === "string" && !argValue)
throw new CliError(
`Command \`jsdoc-md\` argument \`${argName}\` requires a value.`
);
const {
"--source-glob": sourceGlob,
"--markdown-path": markdownPath,
"--target-heading": targetHeading,
"--check": check,
} = expectedArgs;
await jsdocMd({ sourceGlob, markdownPath, targetHeading, check });
} catch (error) {
let reportError = error;
if (error instanceof arg.ArgError)
switch (error.code) {
case "ARG_MISSING_REQUIRED_SHORTARG": {
const [, badArgName] = error.message.match(/: (.+?)$/u);
reportError = new CliError(
`Command \`jsdoc-md\` argument \`${badArgName}\` requires a value, but was followed by another short argument.`
);
break;
}
case "ARG_MISSING_REQUIRED_LONGARG": {
const [, badArgName, aliasForArgName] = error.message.match(
/: (.+?)(?: \(alias for (.+?)\))?$/u
);
reportError = new CliError(
`Command \`jsdoc-md\` argument \`${badArgName}\`${
aliasForArgName ? ` (alias for \`${aliasForArgName}\`)` : ""
} requires a value.`
);
}
}
reportCliError("jsdoc-md", reportError);
process.exitCode = 1;
}
}
jsdocMdCli();