@@ -4,21 +4,32 @@ import * as commander from "@commander-js/extra-typings";
44
55import { UsageError } from "./errors.js" ;
66
7- function wrapAction < Command extends commander . Command , Args extends unknown [ ] > (
8- fn : ( this : Command , ...args : Args ) => void | Promise < void > ,
9- ) {
10- return async function ( this : Command , ...args : Args ) {
7+ export function wrapAction <
8+ Args extends unknown [ ] ,
9+ Opts extends commander . OptionValues ,
10+ GlobalOpts extends commander . OptionValues ,
11+ Command extends commander . Command < Args , Opts , GlobalOpts > ,
12+ ActionArgs extends unknown [ ] ,
13+ > ( fn : ( this : Command , ...args : ActionArgs ) => void | Promise < void > ) {
14+ return async function ( this : Command , ...args : ActionArgs ) {
1115 try {
1216 await fn . call ( this , ...args ) ;
1317 } catch ( error ) {
1418 process . exitCode = 1 ;
1519 if ( error instanceof SpawnFailure ) {
1620 error . flushOutput ( "both" ) ;
21+ } else if (
22+ error instanceof Error &&
23+ error . cause instanceof SpawnFailure
24+ ) {
25+ error . cause . flushOutput ( "both" ) ;
1726 }
27+ // Ensure some visual distance to the previous output
28+ console . error ( ) ;
1829 if ( error instanceof UsageError || error instanceof SpawnFailure ) {
1930 console . error ( chalk . red ( "ERROR" ) , error . message ) ;
2031 if ( error . cause instanceof Error ) {
21- console . error ( chalk . red ( "CAUSE" ) , error . cause . message ) ;
32+ console . error ( chalk . blue ( "CAUSE" ) , error . cause . message ) ;
2233 }
2334 if ( error instanceof UsageError && error . fix ) {
2435 console . error (
@@ -34,17 +45,3 @@ function wrapAction<Command extends commander.Command, Args extends unknown[]>(
3445 }
3546 } ;
3647}
37-
38- import { Command } from "@commander-js/extra-typings" ;
39-
40- // Patch Command to wrap all actions with our error handler
41-
42- // eslint-disable-next-line @typescript-eslint/unbound-method
43- const originalAction = Command . prototype . action ;
44-
45- Command . prototype . action = function action < Command extends commander . Command > (
46- this : Command ,
47- fn : Parameters < typeof originalAction > [ 0 ] ,
48- ) {
49- return originalAction . call ( this , wrapAction ( fn ) ) ;
50- } ;
0 commit comments