Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/cli-repl/src/mongosh-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type { MongoshIOProvider, MongoshNodeReplOptions } from './mongosh-repl';
import MongoshNodeRepl from './mongosh-repl';
import { parseAnyLogEntry } from '../../shell-api/src/log-entry';
import stripAnsi from 'strip-ansi';
import { formatOutput } from './format-output';

function nonnull<T>(value: T | null | undefined): NonNullable<T> {
if (!value) throw new Error();
Expand Down Expand Up @@ -526,18 +527,53 @@ describe('MongoshNodeRepl', function () {
]) {
context(mode, function () {
let getHistory: () => string[];
let getAllHistoryItems: () => string[];

beforeEach(function () {
const { history } = mongoshRepl.runtimeState().repl as unknown as {
history: string[];
};
getHistory = () =>
history.filter((line) => !line.startsWith('prefill-'));
getAllHistoryItems = () => history;
for (let i = 0; i < prefill; i++) {
history.unshift(`prefill-${i}`);
}
});

describe('history() command', function () {
it('returns a formatted array of history', async function () {
output = '';
input.write('history()\n');
await waitEval(bus);
expect(output).includes(
formatOutput(
{
value: getAllHistoryItems()
.slice(1, getAllHistoryItems().length)
.reverse(),
},
{ colors: true, maxArrayLength: Infinity }
)
);
});

it('works with array operations', async function () {
output = '';
input.write('history().slice(history().length-10).reverse()\n');
await waitEval(bus);
const history = getAllHistoryItems().slice(1).reverse();
expect(output).includes(
formatOutput(
{
value: history.slice(history.length - 10).reverse(),
},
{ colors: false, maxArrayLength: Infinity }
)
);
});
});

it('looks up existing entries, if there are any', async function () {
output = '';
input.write(arrowUp);
Expand Down
28 changes: 28 additions & 0 deletions packages/cli-repl/src/mongosh-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { markTime } from './startup-timing';
import type { Context } from 'vm';
import { Script, createContext, runInContext } from 'vm';
import { installPasteSupport } from './repl-paste-support';
import util from 'util';

declare const __non_webpack_require__: any;

Expand Down Expand Up @@ -365,6 +366,8 @@ class MongoshNodeRepl implements EvaluationListener {
await this.finishInitializingNodeRepl();
instanceState.setCtx(context);

this.setupHistoryCommand();

if (!this.shellCliOptions.nodb && !this.shellCliOptions.quiet) {
// cf. legacy shell:
// https://github.com/mongodb/mongo/blob/a6df396047a77b90bf1ce9463eecffbee16fb864/src/mongo/shell/mongo_main.cpp#L1003-L1026
Expand All @@ -391,6 +394,31 @@ class MongoshNodeRepl implements EvaluationListener {
return { __initialized: 'yes' };
}

setupHistoryCommand(): void {
const history = () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const replHistory: string[] = (this.runtimeState().repl as any).history;
const formattedHistory =
// Remove the history call from the formatted history
replHistory.slice(1).reverse();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
formattedHistory[util.inspect.custom as any] = (() => {
return formatOutput(
{
// Providing a copy of the history avoids a circular reference.
value: formattedHistory.concat(),
},
{ colors: true, maxArrayLength: Infinity }
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any;
return formattedHistory;
};

this.runtimeState().context.history = history;
}

private async finishInitializingNodeRepl(): Promise<void> {
const { repl, instanceState } = this.runtimeState();
if (!repl) return;
Expand Down
Loading