Skip to content

Commit 761eeca

Browse files
committed
refactor: move to mongosh-repl and use a hotfixed array
1 parent c5deb64 commit 761eeca

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

packages/cli-repl/src/cli-repl.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ import type {
5454
DevtoolsProxyOptions,
5555
} from '@mongodb-js/devtools-proxy-support';
5656
import { useOrCreateAgent } from '@mongodb-js/devtools-proxy-support';
57-
import type { TypeSignature } from '@mongosh/shell-api';
58-
import { signatures } from '@mongosh/shell-api';
59-
import util from 'util';
60-
import { formatOutput } from './format-output';
6157

6258
/**
6359
* Connecting text key.
@@ -500,8 +496,6 @@ export class CliRepl implements MongoshIOProvider {
500496
});
501497
}
502498

503-
this.setupHistoryCommand();
504-
505499
Editor.create({
506500
input: this.input,
507501
vscodeDir: this.shellHomeDirectory.rcPath('.vscode'),
@@ -916,47 +910,6 @@ export class CliRepl implements MongoshIOProvider {
916910
return this.shellHomeDirectory.roamingPath('mongosh_repl_history');
917911
}
918912

919-
/** Returns the history of commands. */
920-
setupHistoryCommand(): void {
921-
const getHistory = () => {
922-
const replHistory: string[] = (
923-
this.mongoshRepl.runtimeState().repl as any
924-
).history;
925-
const formattedHistory =
926-
// Remove the history call from the formatted history
927-
replHistory.slice(1, replHistory.length).reverse();
928-
929-
return {
930-
...formattedHistory,
931-
[util.inspect.custom]: () => {
932-
return formatOutput(
933-
{
934-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
935-
value: formattedHistory,
936-
},
937-
{ colors: true, maxArrayLength: Infinity }
938-
);
939-
},
940-
};
941-
};
942-
943-
getHistory.isDirectShellCommand = true;
944-
getHistory.returnsPromise = true;
945-
getHistory.acceptsRawInput = true;
946-
947-
this.mongoshRepl.runtimeState().context.history =
948-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
949-
(this.mongoshRepl.runtimeState().instanceState.shellApi as any).history =
950-
getHistory;
951-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
952-
(signatures.ShellApi.attributes as any).history = {
953-
type: 'function',
954-
returnsPromise: true,
955-
isDirectShellCommand: true,
956-
acceptsRawInput: true,
957-
} as TypeSignature;
958-
}
959-
960913
/**
961914
* Implements getConfig from the {@link ConfigProvider} interface.
962915
*/

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import type {
99
EvaluationListener,
1010
OnLoadResult,
1111
ShellCliOptions,
12+
TypeSignature,
1213
} from '@mongosh/shell-api';
1314
import {
1415
ShellInstanceState,
1516
getShellApiType,
17+
signatures,
1618
toShellResult,
1719
} from '@mongosh/shell-api';
1820
import type { ShellResult } from '@mongosh/shell-evaluator';
@@ -47,6 +49,7 @@ import { markTime } from './startup-timing';
4749
import type { Context } from 'vm';
4850
import { Script, createContext, runInContext } from 'vm';
4951
import { installPasteSupport } from './repl-paste-support';
52+
import util from 'util';
5053

5154
declare const __non_webpack_require__: any;
5255

@@ -365,6 +368,8 @@ class MongoshNodeRepl implements EvaluationListener {
365368
await this.finishInitializingNodeRepl();
366369
instanceState.setCtx(context);
367370

371+
this.setupHistoryCommand();
372+
368373
if (!this.shellCliOptions.nodb && !this.shellCliOptions.quiet) {
369374
// cf. legacy shell:
370375
// https://github.com/mongodb/mongo/blob/a6df396047a77b90bf1ce9463eecffbee16fb864/src/mongo/shell/mongo_main.cpp#L1003-L1026
@@ -391,6 +396,44 @@ class MongoshNodeRepl implements EvaluationListener {
391396
return { __initialized: 'yes' };
392397
}
393398

399+
setupHistoryCommand(): void {
400+
const getHistory = () => {
401+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
402+
const replHistory: string[] = (this.runtimeState().repl as any).history;
403+
const formattedHistory =
404+
// Remove the history call from the formatted history
405+
replHistory.slice(1, replHistory.length).reverse();
406+
407+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
408+
formattedHistory[util.inspect.custom as any] = (() => {
409+
return formatOutput(
410+
{
411+
// The value of the format has to be a copy of the history to avoid circular references.
412+
value: formattedHistory.concat(),
413+
},
414+
{ colors: true, maxArrayLength: Infinity }
415+
);
416+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
417+
}) as any;
418+
return formattedHistory;
419+
};
420+
421+
getHistory.isDirectShellCommand = true;
422+
getHistory.returnsPromise = false;
423+
getHistory.acceptsRawInput = true;
424+
425+
this.runtimeState().context.history =
426+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
427+
(this.runtimeState().instanceState.shellApi as any).history = getHistory;
428+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
429+
(signatures.ShellApi.attributes as any).history = {
430+
type: 'function',
431+
returnsPromise: true,
432+
isDirectShellCommand: true,
433+
acceptsRawInput: true,
434+
} as TypeSignature;
435+
}
436+
394437
private async finishInitializingNodeRepl(): Promise<void> {
395438
const { repl, instanceState } = this.runtimeState();
396439
if (!repl) return;

0 commit comments

Comments
 (0)