Skip to content

Commit 52ac329

Browse files
committed
fixup: restructure, add comments
1 parent b69fba2 commit 52ac329

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { ReadLineOptions } from 'readline';
55
import type { ReplOptions, REPLServer } from 'repl';
66
import type { start as originalStart } from 'repl';
77
import { promisify } from 'util';
8-
import type { KeypressKey } from './repl-paste-support';
8+
import { prototypeChain, type KeypressKey } from './repl-paste-support';
99

1010
// Utility, inverse of Readonly<T>
1111
type Mutable<T> = {
@@ -407,3 +407,39 @@ function wrapPauseInput<Args extends any[], Ret>(
407407
}
408408
};
409409
}
410+
411+
// Not related to paste support, but rather for integrating with the MongoshNodeRepl's
412+
// line-by-line input handling. Calling this methods adds hooks to `repl` that are called
413+
// when the REPL is ready to evaluate further input. Eventually, like the other code
414+
// in this file, we should upstream this into Node.js core and/or evaluate the need for
415+
// it entirely.
416+
export function addReplEventForEvalReady(
417+
repl: REPLServer,
418+
before: () => boolean,
419+
after: () => void
420+
): void {
421+
const wrapMethodWithLineByLineInputNextLine = (
422+
repl: REPLServer,
423+
key: keyof REPLServer
424+
) => {
425+
if (!repl[key]) return;
426+
const originalMethod = repl[key].bind(repl);
427+
(repl as any)[key] = (...args: any[]) => {
428+
if (!before()) {
429+
return;
430+
}
431+
const result = originalMethod(...args);
432+
after();
433+
return result;
434+
};
435+
};
436+
// https://github.com/nodejs/node/blob/88f4cef8b96b2bb9d4a92f6848ce4d63a82879a8/lib/internal/readline/interface.js#L954
437+
// added in https://github.com/nodejs/node/commit/96be7836d794509dd455e66d91c2975419feed64
438+
// handles newlines inside multi-line input and replaces `.displayPrompt()` which was
439+
// previously used to print the prompt for multi-line input.
440+
const addNewLineOnTTYKey = [...prototypeChain(repl)]
441+
.flatMap((proto) => Object.getOwnPropertySymbols(proto))
442+
.find((s) => String(s).includes('(_addNewLineOnTTY)')) as keyof REPLServer;
443+
wrapMethodWithLineByLineInputNextLine(repl, 'displayPrompt');
444+
wrapMethodWithLineByLineInputNextLine(repl, addNewLineOnTTYKey);
445+
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ import type { FormatOptions } from './format-output';
4747
import { markTime } from './startup-timing';
4848
import type { Context } from 'vm';
4949
import { Script, createContext, runInContext } from 'vm';
50-
import {
51-
addReplEventForEvalReady,
52-
installPasteSupport,
53-
} from './repl-paste-support';
50+
import { installPasteSupport } from './repl-paste-support';
5451
import util from 'util';
5552

5653
declare const __non_webpack_require__: any;
@@ -524,7 +521,7 @@ class MongoshNodeRepl implements EvaluationListener {
524521
// This is used below for multiline history manipulation.
525522
let originalHistory: string[] | null = null;
526523

527-
addReplEventForEvalReady(
524+
asyncRepl.addReplEventForEvalReady(
528525
repl,
529526
() => !!this.started,
530527
() => this.lineByLineInput.nextLine()

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type KeypressKey = {
1111
code?: string;
1212
};
1313

14-
function* prototypeChain(obj: unknown): Iterable<unknown> {
14+
export function* prototypeChain(obj: unknown): Iterable<unknown> {
1515
if (!obj) return;
1616
yield obj;
1717
yield* prototypeChain(Object.getPrototypeOf(obj));
@@ -65,36 +65,3 @@ export function installPasteSupport(repl: REPLServer): string {
6565
});
6666
return onEnd;
6767
}
68-
69-
// Not related to paste support, but rather for integrating with the MongoshNodeRepl's
70-
// line-by-line input handling.
71-
export function addReplEventForEvalReady(
72-
repl: REPLServer,
73-
before: () => boolean,
74-
after: () => void
75-
): void {
76-
const wrapMethodWithLineByLineInputNextLine = (
77-
repl: REPLServer,
78-
key: keyof REPLServer
79-
) => {
80-
if (!repl[key]) return;
81-
const originalMethod = repl[key].bind(repl);
82-
(repl as any)[key] = (...args: any[]) => {
83-
if (!before()) {
84-
return;
85-
}
86-
const result = originalMethod(...args);
87-
after();
88-
return result;
89-
};
90-
};
91-
// https://github.com/nodejs/node/blob/88f4cef8b96b2bb9d4a92f6848ce4d63a82879a8/lib/internal/readline/interface.js#L954
92-
// added in https://github.com/nodejs/node/commit/96be7836d794509dd455e66d91c2975419feed64
93-
// handles newlines inside multi-line input and replaces `.displayPrompt()` which was
94-
// previously used to print the prompt for multi-line input.
95-
const addNewLineOnTTYKey = [...prototypeChain(repl)]
96-
.flatMap((proto) => Object.getOwnPropertySymbols(proto))
97-
.find((s) => String(s).includes('(_addNewLineOnTTY)')) as keyof REPLServer;
98-
wrapMethodWithLineByLineInputNextLine(repl, 'displayPrompt');
99-
wrapMethodWithLineByLineInputNextLine(repl, addNewLineOnTTYKey);
100-
}

0 commit comments

Comments
 (0)