Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser-repl)!: keep operation in progress COMPASS-8576 MONGOSH-1966 #2284

Merged
merged 33 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a339feb
Don't lose the isOperationInProgress state when the user switches tabs
lerouxb Nov 28, 2024
6e7d247
minus comment
lerouxb Nov 28, 2024
98430ee
initial like the others for now
lerouxb Dec 4, 2024
4a95d91
progress towards moving Shell to be a function component
lerouxb Dec 4, 2024
5d48859
typo
lerouxb Dec 4, 2024
8587f0d
readme tweaks
lerouxb Dec 4, 2024
2e85187
rm comment
lerouxb Dec 4, 2024
8ca4f52
browser-repl tests using testing library
lerouxb Dec 6, 2024
5ab819b
Merge branch 'main' into keep-operation-in-progress2
lerouxb Dec 6, 2024
45946cb
Update packages/browser-repl/scripts/sync-to-compass.js
lerouxb Dec 9, 2024
4a7f4e9
Update packages/browser-repl/scripts/sync-to-compass.js
lerouxb Dec 9, 2024
5cf7a44
Update packages/browser-repl/src/components/shell.tsx
lerouxb Dec 9, 2024
e3bf5f7
Update packages/browser-repl/src/components/shell.tsx
lerouxb Dec 9, 2024
a87e420
Update packages/browser-repl/src/components/shell.tsx
lerouxb Dec 9, 2024
85143c0
always clear on enter
lerouxb Dec 9, 2024
e54962a
lint
lerouxb Dec 9, 2024
50313a7
Merge branch 'main' into keep-operation-in-progress2
lerouxb Dec 9, 2024
7c3a692
remove darkMode
lerouxb Dec 11, 2024
e24af9f
move the eslint comments
lerouxb Dec 11, 2024
3567fcb
update readme
lerouxb Dec 11, 2024
705f4ff
Merge branch 'main' into keep-operation-in-progress2
lerouxb Dec 11, 2024
be07c02
keep the text value as initialText
lerouxb Dec 11, 2024
686cbc9
and the sandbox
lerouxb Dec 11, 2024
b34b9a4
use shell input container ref
lerouxb Dec 12, 2024
757ef7c
cap history before redacting
lerouxb Dec 12, 2024
72d16b3
unnecessary lint comment
lerouxb Dec 12, 2024
72a438b
editor ref
lerouxb Dec 12, 2024
d0b920d
test fixes
lerouxb Dec 12, 2024
9b15c06
unnecessary import
lerouxb Dec 12, 2024
add38a0
more stable
lerouxb Dec 12, 2024
6f20f37
don't re-run initialEvaluate AND don't lose the output
lerouxb Dec 12, 2024
37e3c8b
optimisation: don't get the shell prompt before executing initialEval…
lerouxb Dec 12, 2024
4e66b19
use refs and update them
lerouxb Dec 13, 2024
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
3 changes: 2 additions & 1 deletion packages/browser-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"depcheck": "depcheck",
"compile": "tsc -p tsconfig.json",
"prettier": "prettier",
"reformat": "npm run prettier -- --write . && npm run eslint --fix"
"reformat": "npm run prettier -- --write . && npm run eslint --fix",
"sync-to-compass": "node scripts/sync-to-compass.js"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful in combination with this. Based on https://github.com/mongodb-js/compass/blob/main/packages/compass-web/scripts/sync-dist-to-mms.js

This way you can save a file in browser-repl in mongosh and it hot-reloads in compass.

},
"config": {
"unsafe-perm": true
Expand Down
57 changes: 57 additions & 0 deletions packages/browser-repl/scripts/sync-to-compass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable no-console */
'use strict';
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const { debounce } = require('lodash');
lerouxb marked this conversation as resolved.
Show resolved Hide resolved

if (!process.env.COMPASS_HOME) {
throw new Error('Missing required environment variable $COMPASS_HOME.');
}

const packageDir = path.resolve(__dirname, '..');
const srcDir = path.resolve(__dirname, '..', 'src');
const libDir = path.resolve(__dirname, '..', 'lib');

const destDir = path.dirname(
child_process.execFileSync(
'node',
['-e', "console.log(require.resolve('@mongosh/browser-repl'))"],
lerouxb marked this conversation as resolved.
Show resolved Hide resolved
{ cwd: process.env.COMPASS_HOME, encoding: 'utf-8' }
)
);

console.log({ packageDir, srcDir, libDir, destDir });

const compileAndCopy = debounce(
function () {
child_process.execFileSync('npm', ['run', 'compile'], { cwd: packageDir });
fs.cpSync(libDir, destDir, { recursive: true });
console.log('done.');
},
1_000,
{
leading: true,
trailing: true,
}
);

const srcWatcher = fs.watch(
srcDir,
{ recursive: true },
function (eventType, filename) {
console.log(eventType, filename);
compileAndCopy();
}
);

function cleanup() {
srcWatcher.close();
}

for (const evt of ['SIGINT', 'SIGTERM']) {
process.on(evt, cleanup);
}

// do an initial copy on startup
compileAndCopy();
8 changes: 5 additions & 3 deletions packages/browser-repl/src/components/shell-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ export class ShellInput extends Component<ShellInputProps, ShellInputState> {

private onEnter = async (): Promise<void> => {
if (this.props.onInput) {
await this.props.onInput(this.state.currentValue);
const value = this.state.currentValue;
// clear the value before evaluating the input because it could take a
// long time
this.setState({ currentValue: '' });
lerouxb marked this conversation as resolved.
Show resolved Hide resolved
await this.props.onInput(value);
}

this.setState({ currentValue: '' });
};

render(): JSX.Element {
Expand Down
22 changes: 8 additions & 14 deletions packages/browser-repl/src/components/shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ interface ShellProps {
*/
initialHistory: readonly string[];

/**
* A boolean so we can keep the isOperationInProgress state between sessions.
lerouxb marked this conversation as resolved.
Show resolved Hide resolved
*/
isOperationInProgress?: boolean;

darkMode?: boolean;

className?: string;
}

interface ShellState {
operationInProgress: boolean;
output: readonly ShellOutputEntry[];
history: readonly string[];
passwordPrompt: string;
Expand All @@ -153,14 +157,6 @@ const normalizeInitialEvaluate = (initialEvaluate: string | string[]) => {
});
};

const isInitialEvaluateEmpty = (
initialEvaluate?: string | string[] | undefined
) => {
return (
!initialEvaluate || normalizeInitialEvaluate(initialEvaluate).length === 0
);
};

/**
* The browser-repl Shell component
*/
Expand All @@ -175,6 +171,7 @@ export class _Shell extends Component<ShellProps, ShellState> {
initialInput: '',
initialOutput: [],
initialHistory: [],
isOperationInProgress: false,
};

private shellInputElement: HTMLElement | null = null;
Expand All @@ -183,7 +180,6 @@ export class _Shell extends Component<ShellProps, ShellState> {
private onCancelPasswordPrompt: () => void = noop;

readonly state: ShellState = {
operationInProgress: !isInitialEvaluateEmpty(this.props.initialEvaluate),
output: this.props.initialOutput.slice(-this.props.maxOutputLength),
history: this.props.initialHistory.slice(0, this.props.maxHistoryLength),
passwordPrompt: '',
Expand Down Expand Up @@ -357,7 +353,6 @@ export class _Shell extends Component<ShellProps, ShellState> {

let output = this.addEntriesToOutput([inputLine]);
this.setState({
operationInProgress: true,
output,
});
this.props.onOutputChanged(output);
Expand All @@ -367,7 +362,6 @@ export class _Shell extends Component<ShellProps, ShellState> {
output = this.addEntriesToOutput([outputLine]);
const history = this.addEntryToHistory(code);
this.setState({
operationInProgress: false,
output,
history,
});
Expand Down Expand Up @@ -411,7 +405,7 @@ export class _Shell extends Component<ShellProps, ShellState> {

private onSigInt = (): Promise<boolean> => {
if (
this.state.operationInProgress &&
this.props.isOperationInProgress &&
(this.props.runtime as WorkerRuntime).interrupt
) {
return (this.props.runtime as WorkerRuntime).interrupt();
Expand Down Expand Up @@ -440,7 +434,7 @@ export class _Shell extends Component<ShellProps, ShellState> {
history={this.state.history}
onClearCommand={this.onClearCommand}
onInput={this.onInput}
operationInProgress={this.state.operationInProgress}
operationInProgress={this.props.isOperationInProgress}
editorRef={this.setEditor}
onSigInt={this.onSigInt}
/>
Expand Down
Loading