File tree Expand file tree Collapse file tree 2 files changed +10
-2
lines changed
Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -7,10 +7,15 @@ import { handleCliError } from "./errors.js";
77
88installTelemetry ( cli ) ;
99
10+ function resolveOutputFormat ( ) : "tui" | "json" {
11+ const opts = cli . optsWithGlobals ?. ( ) ;
12+ if ( opts ?. output === "tui" || opts ?. output === "json" ) return opts . output ;
13+ return process . stderr . isTTY ? "tui" : "json" ;
14+ }
15+
1016try {
1117 await cli . parseAsync ( ) ;
1218 await Promise . all ( [ checkAutoUpdate ( ) , flushTelemetry ( ) ] ) ;
1319} catch ( err ) {
14- const format = ( cli . optsWithGlobals ?. ( ) . output ?? ( process . stderr . isTTY ? "tui" : "json" ) ) as "tui" | "json" ;
15- handleCliError ( err , format ) ;
20+ handleCliError ( err , resolveOutputFormat ( ) ) ;
1621}
Original file line number Diff line number Diff line change @@ -22,20 +22,23 @@ describe("handleCliError", () => {
2222
2323 test ( "prints json error when format is json" , ( ) => {
2424 expect ( ( ) => handleCliError ( new Error ( "boom" ) , "json" ) ) . toThrow ( "exit:1" ) ;
25+ expect ( exitSpy ) . toHaveBeenCalledWith ( 1 ) ;
2526 expect ( errorSpy ) . toHaveBeenCalledWith ( JSON . stringify ( { error : { message : "boom" } } ) ) ;
2627 } ) ;
2728
2829 test ( "prints plain text when format is tui and stderr is not a TTY" , ( ) => {
2930 Object . defineProperty ( process . stderr , "isTTY" , { value : false , configurable : true } ) ;
3031
3132 expect ( ( ) => handleCliError ( new Error ( "oops" ) , "tui" ) ) . toThrow ( "exit:1" ) ;
33+ expect ( exitSpy ) . toHaveBeenCalledWith ( 1 ) ;
3234 expect ( errorSpy ) . toHaveBeenCalledWith ( "Error: oops" ) ;
3335 } ) ;
3436
3537 test ( "prints boxed error when format is tui and stderr is a TTY" , ( ) => {
3638 Object . defineProperty ( process . stderr , "isTTY" , { value : true , configurable : true } ) ;
3739
3840 expect ( ( ) => handleCliError ( new Error ( "boxed" ) , "tui" ) ) . toThrow ( "exit:1" ) ;
41+ expect ( exitSpy ) . toHaveBeenCalledWith ( 1 ) ;
3942 const output = errorSpy . mock . calls . map ( ( c ) => c [ 0 ] ) . join ( "" ) ;
4043 expect ( output ) . toContain ( "boxed" ) ;
4144 expect ( output ) . toContain ( "Error" ) ;
You can’t perform that action at this time.
0 commit comments