@@ -97,7 +97,7 @@ class CliRepl {
97
97
*/
98
98
async connect ( driverUri : string , driverOptions : NodeOptions ) : Promise < any > {
99
99
if ( ! this . options . nodb ) {
100
- console . log ( i18n . __ ( CONNECTING ) , ' ' , clr ( retractPassword ( driverUri ) , [ 'bold' , 'green' ] ) ) ;
100
+ console . log ( i18n . __ ( CONNECTING ) , ' ' , this . clr ( retractPassword ( driverUri ) , [ 'bold' , 'green' ] ) ) ;
101
101
}
102
102
return await CliServiceProvider . connect ( driverUri , driverOptions , this . options ) ;
103
103
}
@@ -116,9 +116,9 @@ class CliRepl {
116
116
prompt : '> ' ,
117
117
writer : this . writer ,
118
118
completer : completer . bind ( null , version ) ,
119
- terminal : true ,
120
119
breakEvalOnSigint : true ,
121
120
preview : false ,
121
+ terminal : process . env . MONGOSH_FORCE_TERMINAL ? true : undefined
122
122
} ) ;
123
123
124
124
const originalDisplayPrompt = this . repl . displayPrompt . bind ( this . repl ) ;
@@ -128,12 +128,14 @@ class CliRepl {
128
128
this . lineByLineInput . nextLine ( ) ;
129
129
} ;
130
130
131
- const originalEditorAction = this . repl . commands . editor . action . bind ( this . repl ) ;
131
+ if ( this . repl . commands . editor ) {
132
+ const originalEditorAction = this . repl . commands . editor . action . bind ( this . repl ) ;
132
133
133
- this . repl . commands . editor . action = ( ) : any => {
134
- this . lineByLineInput . disableBlockOnNewline ( ) ;
135
- return originalEditorAction ( ) ;
136
- } ;
134
+ this . repl . commands . editor . action = ( ) : any => {
135
+ this . lineByLineInput . disableBlockOnNewline ( ) ;
136
+ return originalEditorAction ( ) ;
137
+ } ;
138
+ }
137
139
138
140
this . repl . defineCommand ( 'clear' , {
139
141
help : '' ,
@@ -355,10 +357,10 @@ class CliRepl {
355
357
stack : result . stack
356
358
} ;
357
359
this . bus . emit ( 'mongosh:error' , output ) ;
358
- return formatOutput ( { type : 'Error' , value : output } ) ;
360
+ return this . formatOutput ( { type : 'Error' , value : output } ) ;
359
361
}
360
362
361
- return formatOutput ( { type : result . type , value : result . printable } ) ;
363
+ return this . formatOutput ( { type : result . type , value : result . printable } ) ;
362
364
} ;
363
365
364
366
verifyNodeVersion ( ) : void {
@@ -377,7 +379,7 @@ class CliRepl {
377
379
greet ( ) : void {
378
380
const { version } = require ( '../package.json' ) ;
379
381
console . log ( `Using MongoDB: ${ this . internalState . connectionInfo . buildInfo . version } ` ) ;
380
- console . log ( `${ clr ( 'Using Mongosh Beta' , [ 'bold' , 'yellow' ] ) } : ${ version } ` ) ;
382
+ console . log ( `${ this . clr ( 'Using Mongosh Beta' , [ 'bold' , 'yellow' ] ) } : ${ version } ` ) ;
381
383
console . log ( `${ MONGOSH_WIKI } ` ) ;
382
384
if ( ! this . disableGreetingMessage ) console . log ( TELEMETRY ) ;
383
385
}
@@ -410,7 +412,7 @@ class CliRepl {
410
412
read ( readOptions , ( error , password ) => {
411
413
if ( error ) {
412
414
this . bus . emit ( 'mongosh:error' , error ) ;
413
- return console . log ( formatError ( error ) ) ;
415
+ return console . log ( this . formatError ( error ) ) ;
414
416
}
415
417
416
418
driverOptions . auth . password = password ;
@@ -465,7 +467,7 @@ class CliRepl {
465
467
this . bus . emit ( 'mongosh:error' , error ) ;
466
468
}
467
469
468
- console . error ( formatError ( error ) ) ;
470
+ console . error ( this . formatError ( error ) ) ;
469
471
return process . exit ( 1 ) ;
470
472
}
471
473
@@ -475,6 +477,25 @@ class CliRepl {
475
477
// https://github.com/DefinitelyTyped/DefinitelyTyped/pull/48646
476
478
( this . repl as any ) . output . write ( joined + '\n' ) ;
477
479
}
480
+
481
+ formatOutput ( value : any ) : string {
482
+ return formatOutput ( value , this . getFormatOptions ( ) ) ;
483
+ }
484
+
485
+ formatError ( value : any ) : string {
486
+ return formatError ( value , this . getFormatOptions ( ) ) ;
487
+ }
488
+
489
+ clr ( text : string , style : string | string [ ] ) : string {
490
+ return clr ( text , style , this . getFormatOptions ( ) ) ;
491
+ }
492
+
493
+ getFormatOptions ( ) : { colors : boolean } {
494
+ return {
495
+ colors : this . repl ? this . repl . useColors :
496
+ process . stdout . isTTY && process . stdout . getColorDepth ( ) > 1
497
+ } ;
498
+ }
478
499
}
479
500
480
501
export default CliRepl ;
0 commit comments