11import { SpawnFailure } from "bufout" ;
22import chalk from "chalk" ;
3+ import * as commander from "@commander-js/extra-typings" ;
34
45import { UsageError } from "./errors.js" ;
56
6- export function wrapAction < Args extends unknown [ ] > (
7- action : ( ...args : Args ) => void | Promise < void > ,
8- ) : ( ... args : Args ) => Promise < void > {
9- return async ( ...args : Args ) => {
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 ) {
1011 try {
11- await action ( ...args ) ;
12+ await fn . call ( this , ...args ) ;
1213 } catch ( error ) {
1314 process . exitCode = 1 ;
1415 if ( error instanceof SpawnFailure ) {
@@ -33,3 +34,17 @@ export function wrapAction<Args extends unknown[]>(
3334 }
3435 } ;
3536}
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