1
1
import completer from '@mongosh/autocomplete' ;
2
2
import { MongoshCommandFailed , MongoshInternalError , MongoshWarning } from '@mongosh/errors' ;
3
3
import { changeHistory } from '@mongosh/history' ;
4
- import i18n from '@mongosh/i18n' ;
5
4
import type { ServiceProvider , AutoEncryptionOptions } from '@mongosh/service-provider-core' ;
6
5
import { EvaluationListener , ShellCliOptions , ShellInternalState , OnLoadResult } from '@mongosh/shell-api' ;
7
6
import { ShellEvaluator , ShellResult } from '@mongosh/shell-evaluator' ;
8
- import type { MongoshBus , UserConfig } from '@mongosh/types' ;
7
+ import type { MongoshBus , CliUserConfig , ConfigProvider } from '@mongosh/types' ;
9
8
import askpassword from 'askpassword' ;
10
9
import { Console } from 'console' ;
11
10
import { once } from 'events' ;
@@ -25,10 +24,8 @@ export type MongoshCliOptions = ShellCliOptions & {
25
24
quiet ?: boolean ;
26
25
} ;
27
26
28
- export type MongoshIOProvider = {
27
+ export type MongoshIOProvider = ConfigProvider < CliUserConfig > & {
29
28
getHistoryFilePath ( ) : string ;
30
- getConfig < K extends keyof UserConfig > ( key : K ) : Promise < UserConfig [ K ] > ;
31
- setConfig < K extends keyof UserConfig > ( key : K , value : UserConfig [ K ] ) : Promise < void > ;
32
29
exit ( code : number ) : Promise < never > ;
33
30
readFileUTF8 ( filename : string ) : Promise < { contents : string , absolutePath : string } > ;
34
31
startMongocryptd ( ) : Promise < AutoEncryptionOptions [ 'extraOptions' ] > ;
@@ -72,6 +69,7 @@ class MongoshNodeRepl implements EvaluationListener {
72
69
ioProvider : MongoshIOProvider ;
73
70
onClearCommand ?: EvaluationListener [ 'onClearCommand' ] ;
74
71
insideAutoComplete : boolean ;
72
+ inspectDepth = 0 ;
75
73
76
74
constructor ( options : MongoshNodeReplOptions ) {
77
75
this . input = options . input ;
@@ -95,6 +93,8 @@ class MongoshNodeRepl implements EvaluationListener {
95
93
await this . greet ( mongodVersion ) ;
96
94
await this . printStartupLog ( internalState ) ;
97
95
96
+ this . inspectDepth = await this . getConfig ( 'inspectDepth' ) ;
97
+
98
98
const repl = asyncRepl . start ( {
99
99
start : prettyRepl . start ,
100
100
input : this . lineByLineInput as unknown as Readable ,
@@ -104,7 +104,7 @@ class MongoshNodeRepl implements EvaluationListener {
104
104
breakEvalOnSigint : true ,
105
105
preview : false ,
106
106
asyncEval : this . eval . bind ( this ) ,
107
- historySize : 1000 , // Same as the old shell.
107
+ historySize : await this . getConfig ( 'historyLength' ) ,
108
108
wrapCallbackError :
109
109
( err : Error ) => Object . assign ( new MongoshInternalError ( err . message ) , { stack : err . stack } ) ,
110
110
...this . nodeReplOptions
@@ -262,9 +262,9 @@ class MongoshNodeRepl implements EvaluationListener {
262
262
text += `Using MongoDB: ${ mongodVersion } \n` ;
263
263
text += `${ this . clr ( 'Using Mongosh Beta' , [ 'bold' , 'yellow' ] ) } : ${ version } \n` ;
264
264
text += `${ MONGOSH_WIKI } \n` ;
265
- if ( ! await this . ioProvider . getConfig ( 'disableGreetingMessage' ) ) {
265
+ if ( ! await this . getConfig ( 'disableGreetingMessage' ) ) {
266
266
text += `${ TELEMETRY_GREETING_MESSAGE } \n` ;
267
- await this . ioProvider . setConfig ( 'disableGreetingMessage' , true ) ;
267
+ await this . setConfig ( 'disableGreetingMessage' , true ) ;
268
268
}
269
269
this . output . write ( text ) ;
270
270
}
@@ -381,16 +381,6 @@ class MongoshNodeRepl implements EvaluationListener {
381
381
return this . formatOutput ( { type : result . type , value : result . printable } ) ;
382
382
}
383
383
384
- async toggleTelemetry ( enabled : boolean ) : Promise < string > {
385
- await this . ioProvider . setConfig ( 'enableTelemetry' , enabled ) ;
386
-
387
- if ( enabled ) {
388
- return i18n . __ ( 'cli-repl.cli-repl.enabledTelemetry' ) ;
389
- }
390
-
391
- return i18n . __ ( 'cli-repl.cli-repl.disabledTelemetry' ) ;
392
- }
393
-
394
384
onPrint ( values : ShellResult [ ] ) : void {
395
385
const joined = values . map ( ( value ) => this . writer ( value ) ) . join ( ' ' ) ;
396
386
this . output . write ( joined + '\n' ) ;
@@ -419,11 +409,12 @@ class MongoshNodeRepl implements EvaluationListener {
419
409
return clr ( text , style , this . getFormatOptions ( ) ) ;
420
410
}
421
411
422
- getFormatOptions ( ) : { colors : boolean } {
412
+ getFormatOptions ( ) : { colors : boolean , depth : number } {
423
413
const output = this . output as WriteStream ;
424
414
return {
425
415
colors : this . _runtimeState ?. repl ?. useColors ??
426
- ( output . isTTY && output . getColorDepth ( ) > 1 )
416
+ ( output . isTTY && output . getColorDepth ( ) > 1 ) ,
417
+ depth : this . inspectDepth
427
418
} ;
428
419
}
429
420
@@ -451,6 +442,24 @@ class MongoshNodeRepl implements EvaluationListener {
451
442
return this . ioProvider . exit ( 0 ) ;
452
443
}
453
444
445
+ async getConfig < K extends keyof CliUserConfig > ( key : K ) : Promise < CliUserConfig [ K ] > {
446
+ return this . ioProvider . getConfig ( key ) ;
447
+ }
448
+
449
+ async setConfig < K extends keyof CliUserConfig > ( key : K , value : CliUserConfig [ K ] ) : Promise < 'success' | 'ignored' > {
450
+ if ( key === 'historyLength' && this . _runtimeState ) {
451
+ ( this . runtimeState ( ) . repl as any ) . historySize = value ;
452
+ }
453
+ if ( key === 'inspectDepth' ) {
454
+ this . inspectDepth = + value ;
455
+ }
456
+ return this . ioProvider . setConfig ( key , value ) ;
457
+ }
458
+
459
+ listConfigOptions ( ) : Promise < string [ ] > | string [ ] {
460
+ return this . ioProvider . listConfigOptions ( ) ;
461
+ }
462
+
454
463
async startMongocryptd ( ) : Promise < AutoEncryptionOptions [ 'extraOptions' ] > {
455
464
return this . ioProvider . startMongocryptd ( ) ;
456
465
}
0 commit comments