@@ -11,6 +11,21 @@ const program = new Command()
11
11
. name ( 'api-docs-tooling' )
12
12
. description ( 'CLI tool to generate and lint Node.js API documentation' ) ;
13
13
14
+ /**
15
+ * Returns a wrapped version of the given async function that catches and rethrows any errors.
16
+ *
17
+ * @function
18
+ * @param {Function } fn - The async function to wrap.
19
+ * @returns {Function } A new function that calls `fn` with any given arguments and rethrows errors.
20
+ */
21
+ const errorWrap =
22
+ fn =>
23
+ ( ...args ) =>
24
+ fn ( ...args ) . catch ( e => {
25
+ console . error ( e ) ;
26
+ process . exit ( 1 ) ;
27
+ } ) ;
28
+
14
29
// Registering generate and lint commands
15
30
commands . forEach ( ( { name, description, options, action } ) => {
16
31
const cmd = program . command ( name ) . description ( description ) ;
@@ -33,21 +48,21 @@ commands.forEach(({ name, description, options, action }) => {
33
48
} ) ;
34
49
35
50
// Set the action for the command
36
- cmd . action ( action ) ;
51
+ cmd . action ( errorWrap ( action ) ) ;
37
52
} ) ;
38
53
39
54
// Register the interactive command
40
55
program
41
56
. command ( 'interactive' )
42
57
. description ( 'Launch guided CLI wizard' )
43
- . action ( interactive ) ;
58
+ . action ( errorWrap ( interactive ) ) ;
44
59
45
60
// Register the list command
46
61
program
47
62
. command ( 'list' )
48
63
. addArgument ( new Argument ( '<types>' , 'The type to list' ) . choices ( types ) )
49
64
. description ( 'List the given type' )
50
- . action ( list ) ;
65
+ . action ( errorWrap ( list ) ) ;
51
66
52
67
// Parse and execute command-line arguments
53
68
program . parse ( process . argv ) ;
0 commit comments