Skip to content

Commit 93271f4

Browse files
authored
MONGOSH-237 - Make print() and console.log() available (#184)
1 parent b5b5d2c commit 93271f4

14 files changed

+161
-127
lines changed

package-lock.json

Lines changed: 66 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,9 @@
821821
"@fortawesome/free-solid-svg-icons": "^5.13.0",
822822
"@fortawesome/react-fontawesome": "^0.1.9",
823823
"@leafygreen-ui/toggle": "3.0.1",
824-
"@mongosh/browser-runtime-electron": "^0.4.2",
825-
"@mongosh/service-provider-server": "^0.4.2",
826-
"@mongosh/shell-api": "^0.4.2",
824+
"@mongosh/browser-runtime-electron": "^0.5.0",
825+
"@mongosh/service-provider-server": "^0.5.0",
826+
"@mongosh/shell-api": "^0.5.0",
827827
"analytics-node": "^3.4.0-beta.1",
828828
"bson": "^4.0.3",
829829
"classnames": "^2.2.6",

src/editors/playgroundController.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import playgroundTemplate from '../templates/playgroundTemplate';
1111
import playgroundSearchTemplate from '../templates/playgroundSearchTemplate';
1212
import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate';
1313
import { createLogger } from '../logging';
14+
import type { ExecuteAllResult } from '../utils/types';
1415

1516
const log = createLogger('playground controller');
1617

@@ -210,9 +211,9 @@ export default class PlaygroundController {
210211
});
211212
}
212213

213-
public async evaluate(codeToEvaluate: string): Promise<any> {
214+
public async evaluate(codeToEvaluate: string): Promise<ExecuteAllResult> {
214215
// Send a request to the language server to execute scripts from a playground.
215-
const result = await this._languageServerController.executeAll(
216+
const result: ExecuteAllResult = await this._languageServerController.executeAll(
216217
codeToEvaluate
217218
);
218219

@@ -223,7 +224,7 @@ export default class PlaygroundController {
223224
result ? false : true
224225
);
225226

226-
return Promise.resolve(result);
227+
return result;
227228
}
228229

229230
private getAllText(): string {
@@ -234,7 +235,7 @@ export default class PlaygroundController {
234235
return this._activeTextEditor?.document.getText(selection) || '';
235236
}
236237

237-
public evaluateWithCancelModal(): Promise<any> {
238+
public evaluateWithCancelModal(): Promise<ExecuteAllResult> {
238239
if (!this._connectionString) {
239240
return Promise.reject(
240241
new Error('Please connect to a database before running a playground.')
@@ -256,19 +257,19 @@ export default class PlaygroundController {
256257
this._outputChannel.clear();
257258
this._outputChannel.show(true);
258259

259-
return resolve(null);
260+
return resolve(undefined);
260261
});
261262

262263
// Run all playground scripts.
263-
const result = await this.evaluate(this._codeToEvaluate);
264+
const result: ExecuteAllResult = await this.evaluate(this._codeToEvaluate);
264265

265266
return resolve(result);
266267
}
267268
)
268269
.then(undefined, (error) => {
269270
log.error('Evaluate playground with cancel modal error', error);
270271

271-
return resolve(null);
272+
return resolve(undefined);
272273
});
273274
});
274275
}
@@ -300,7 +301,7 @@ export default class PlaygroundController {
300301
}
301302
}
302303

303-
const result = await this.evaluateWithCancelModal();
304+
const result: ExecuteAllResult = await this.evaluateWithCancelModal();
304305

305306
if (!result) {
306307
this._outputChannel.clear();
@@ -310,7 +311,8 @@ export default class PlaygroundController {
310311
}
311312

312313
this._outputChannel.clear();
313-
this._outputChannel.append(result);
314+
for (const line of result)
315+
this._outputChannel.appendLine(line.content);
314316
this._outputChannel.show(true);
315317

316318
return resolve(true);

src/language/languageServerController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
CancellationTokenSource
1010
} from 'vscode-languageclient';
1111
import WebSocket from 'ws';
12+
import type { ExecuteAllResult } from '../utils/types';
1213

1314
import { createLogger } from '../logging';
1415
import { ServerCommands, PlaygroundRunParameters } from './serverCommands';
@@ -133,7 +134,7 @@ export default class LanguageServerController {
133134
this.client.stop();
134135
}
135136

136-
public executeAll(codeToEvaluate: string): Promise<any> {
137+
public executeAll(codeToEvaluate: string): Promise<ExecuteAllResult> {
137138
return this.client.onReady().then(() => {
138139
// Instantiate a new CancellationTokenSource object
139140
// that generates a cancellation token for each run of a playground

src/language/mongoDBService.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { CompletionItemKind, CancellationToken } from 'vscode-languageserver';
22
import { Worker as WorkerThreads } from 'worker_threads';
3-
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
4-
import { CliServiceProvider } from '@mongosh/service-provider-server';
53
import { signatures } from '@mongosh/shell-api';
64
import * as util from 'util';
75
import { Visitor } from './visitor';
6+
import { ExecuteAllResult } from '../utils/types';
87

98
import { ServerCommands, PlaygroundRunParameters } from './serverCommands';
109

@@ -14,8 +13,6 @@ const fs = require('fs');
1413
export const languageServerWorkerFileName = 'languageServerWorker.js';
1514

1615
export default class MongoDBService {
17-
_serviceProvider?: CliServiceProvider;
18-
_runtime?: ElectronRuntime;
1916
_connection: any;
2017
_connectionString?: string;
2118
_connectionOptions?: any;
@@ -116,11 +113,6 @@ export default class MongoDBService {
116113
}
117114

118115
try {
119-
this._serviceProvider = await CliServiceProvider.connect(
120-
this._connectionString,
121-
this._connectionOptions
122-
);
123-
this._runtime = new ElectronRuntime(this._serviceProvider);
124116
this.getDatabasesCompletionItems();
125117

126118
return Promise.resolve(true);
@@ -551,8 +543,6 @@ export default class MongoDBService {
551543
}
552544

553545
public clearCurrentSessionConnection(): void {
554-
this._serviceProvider = undefined;
555-
this._runtime = undefined;
556546
this._connectionString = undefined;
557547
this._connectionOptions = undefined;
558548
}

0 commit comments

Comments
 (0)