-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcli.mjs
More file actions
executable file
·70 lines (57 loc) · 1.84 KB
/
cli.mjs
File metadata and controls
executable file
·70 lines (57 loc) · 1.84 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
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
import process from 'node:process';
import { Argument, Command, Option } from 'commander';
import interactive from './commands/interactive.mjs';
import list, { types } from './commands/list.mjs';
import commands from './commands/index.mjs';
const program = new Command()
.name('api-docs-tooling')
.description('CLI tool to generate and lint Node.js API documentation');
/**
* Wraps a function to catch both synchronous and asynchronous errors.
*
* @param {Function} fn - The function to wrap. Can be synchronous or return a Promise.
* @returns {Function} A new function that handles errors and logs them.
*/
const errorWrap =
fn =>
async (...args) => {
try {
return await fn(...args);
} catch (err) {
console.error(err);
process.exit(1);
}
};
// Registering generate and lint commands
commands.forEach(({ name, description, options, action }) => {
const cmd = program.command(name).description(description);
// Add options to the command
Object.values(options).forEach(({ flags, desc, prompt }) => {
const option = new Option(flags.join(', '), desc).default(
prompt.initialValue
);
if (prompt.required) {
option.makeOptionMandatory();
}
if (prompt.type === 'multiselect') {
option.choices(prompt.options.map(({ value }) => value));
}
cmd.addOption(option);
});
// Set the action for the command
cmd.action(errorWrap(action));
});
// Register the interactive command
program
.command('interactive')
.description('Launch guided CLI wizard')
.action(errorWrap(interactive));
// Register the list command
program
.command('list')
.addArgument(new Argument('<types>', 'The type to list').choices(types))
.description('List the given type')
.action(errorWrap(list));
// Parse and execute command-line arguments
program.parse(process.argv);