@@ -11,6 +11,21 @@ const program = new Command()
1111 . name ( 'api-docs-tooling' )
1212 . description ( 'CLI tool to generate and lint Node.js API documentation' ) ;
1313
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+
1429// Registering generate and lint commands
1530commands . forEach ( ( { name, description, options, action } ) => {
1631 const cmd = program . command ( name ) . description ( description ) ;
@@ -33,21 +48,21 @@ commands.forEach(({ name, description, options, action }) => {
3348 } ) ;
3449
3550 // Set the action for the command
36- cmd . action ( action ) ;
51+ cmd . action ( errorWrap ( action ) ) ;
3752} ) ;
3853
3954// Register the interactive command
4055program
4156 . command ( 'interactive' )
4257 . description ( 'Launch guided CLI wizard' )
43- . action ( interactive ) ;
58+ . action ( errorWrap ( interactive ) ) ;
4459
4560// Register the list command
4661program
4762 . command ( 'list' )
4863 . addArgument ( new Argument ( '<types>' , 'The type to list' ) . choices ( types ) )
4964 . description ( 'List the given type' )
50- . action ( list ) ;
65+ . action ( errorWrap ( list ) ) ;
5166
5267// Parse and execute command-line arguments
5368program . parse ( process . argv ) ;
0 commit comments