@@ -4,21 +4,32 @@ import * as commander from "@commander-js/extra-typings";
4
4
5
5
import { UsageError } from "./errors.js" ;
6
6
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 ) {
11
15
try {
12
16
await fn . call ( this , ...args ) ;
13
17
} catch ( error ) {
14
18
process . exitCode = 1 ;
15
19
if ( error instanceof SpawnFailure ) {
16
20
error . flushOutput ( "both" ) ;
21
+ } else if (
22
+ error instanceof Error &&
23
+ error . cause instanceof SpawnFailure
24
+ ) {
25
+ error . cause . flushOutput ( "both" ) ;
17
26
}
27
+ // Ensure some visual distance to the previous output
28
+ console . error ( ) ;
18
29
if ( error instanceof UsageError || error instanceof SpawnFailure ) {
19
30
console . error ( chalk . red ( "ERROR" ) , error . message ) ;
20
31
if ( error . cause instanceof Error ) {
21
- console . error ( chalk . red ( "CAUSE" ) , error . cause . message ) ;
32
+ console . error ( chalk . blue ( "CAUSE" ) , error . cause . message ) ;
22
33
}
23
34
if ( error instanceof UsageError && error . fix ) {
24
35
console . error (
@@ -34,17 +45,3 @@ function wrapAction<Command extends commander.Command, Args extends unknown[]>(
34
45
}
35
46
} ;
36
47
}
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