@@ -47,6 +47,7 @@ import { markTime } from './startup-timing';
4747import type { Context } from 'vm' ;
4848import { Script , createContext , runInContext } from 'vm' ;
4949import { installPasteSupport } from './repl-paste-support' ;
50+ import util from 'util' ;
5051
5152declare const __non_webpack_require__ : any ;
5253
@@ -365,6 +366,8 @@ class MongoshNodeRepl implements EvaluationListener {
365366 await this . finishInitializingNodeRepl ( ) ;
366367 instanceState . setCtx ( context ) ;
367368
369+ this . setupHistoryCommand ( ) ;
370+
368371 if ( ! this . shellCliOptions . nodb && ! this . shellCliOptions . quiet ) {
369372 // cf. legacy shell:
370373 // https://github.com/mongodb/mongo/blob/a6df396047a77b90bf1ce9463eecffbee16fb864/src/mongo/shell/mongo_main.cpp#L1003-L1026
@@ -391,6 +394,35 @@ class MongoshNodeRepl implements EvaluationListener {
391394 return { __initialized : 'yes' } ;
392395 }
393396
397+ setupHistoryCommand ( ) : void {
398+ const history = ( ) => {
399+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
400+ const replHistory : string [ ] = ( this . runtimeState ( ) . repl as any ) . history ;
401+ const formattedHistory =
402+ // Remove the history call from the formatted history
403+ replHistory . slice ( 1 ) . reverse ( ) ;
404+
405+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
406+ formattedHistory [ util . inspect . custom as any ] = ( (
407+ depth : number | null ,
408+ options : util . InspectOptions ,
409+ inspect : typeof util . inspect
410+ ) => {
411+ // We pass a copy of the array without the util.inspect.custom set
412+ // to prevent infinite recursion.
413+ return inspect ( formattedHistory . concat ( ) , {
414+ ...options ,
415+ depth,
416+ maxArrayLength : Infinity ,
417+ } ) ;
418+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
419+ } ) as any ;
420+ return formattedHistory ;
421+ } ;
422+
423+ this . runtimeState ( ) . context . history = history ;
424+ }
425+
394426 private async finishInitializingNodeRepl ( ) : Promise < void > {
395427 const { repl, instanceState } = this . runtimeState ( ) ;
396428 if ( ! repl ) return ;
0 commit comments