Skip to content

Commit eeb174f

Browse files
committed
refactor: extend shell log from shell api
1 parent 3423454 commit eeb174f

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

packages/e2e-tests/test/e2e.spec.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,6 @@ describe('e2e', function () {
578578
shell.assertNoErrors();
579579
});
580580

581-
it('runs a custom log command', async function () {
582-
await shell.executeLine("log.info('Try me')");
583-
shell.assertNoErrors();
584-
});
585-
586581
it('runs help command', async function () {
587582
expect(await shell.executeLine('help')).to.include('Shell Help');
588583
shell.assertNoErrors();
@@ -1514,6 +1509,7 @@ describe('e2e', function () {
15141509

15151510
it('writes custom log directly', async function () {
15161511
await shell.executeLine("log.info('This is a custom entry')");
1512+
expect(shell.assertNoErrors());
15171513
await eventually(async () => {
15181514
const log = await readLogfile();
15191515
expect(
@@ -1531,14 +1527,11 @@ describe('e2e', function () {
15311527
);
15321528
const filename = path.resolve(
15331529
__dirname,
1534-
'..',
1535-
'..',
1536-
'e2e-tests',
1537-
'test',
15381530
'fixtures',
15391531
'custom-log-info.js'
15401532
);
15411533
await shell.executeLine(`load(${JSON.stringify(filename)})`);
1534+
expect(shell.assertNoErrors());
15421535
await eventually(async () => {
15431536
const log = await readLogfile();
15441537
expect(

packages/shell-api/src/shell-instance-state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import NoDatabase from './no-db';
3535
import type { ShellBson } from './shell-bson';
3636
import constructShellBson from './shell-bson';
3737
import { Streams } from './streams';
38-
import constructShellLog, { type ShellLog } from './shell-log';
38+
import ShellLog from './shell-log';
3939

4040
/**
4141
* The subset of CLI options that is relevant for the shell API's behavior itself.
@@ -189,7 +189,7 @@ export default class ShellInstanceState {
189189
this.initialServiceProvider = initialServiceProvider;
190190
this.messageBus = messageBus;
191191
this.shellApi = new ShellApi(this);
192-
this.shellLog = constructShellLog(this.messageBus);
192+
this.shellLog = new ShellLog(this);
193193
this.shellBson = constructShellBson(
194194
initialServiceProvider.bsonLibrary,
195195
(msg: string) => {
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,68 @@
1-
import type { MongoshBus } from '@mongosh/types';
1+
import type ShellInstanceState from './shell-instance-state';
2+
import { shellApiClassDefault, ShellApiClass } from './decorators';
23

3-
export interface ShellLog {
4+
const instanceStateSymbol = Symbol.for('@@mongosh.instanceState');
5+
6+
/**
7+
* This class contains the *global log* property that is considered part of the immediate shell API.
8+
*/
9+
@shellApiClassDefault
10+
export default class ShellLog extends ShellApiClass {
11+
// Use symbols to make sure these are *not* among the things copied over into
12+
// the global scope.
13+
[instanceStateSymbol]: ShellInstanceState;
414
log: {
515
info: (message: string, attr?: unknown) => void;
616
warn: (message: string, attr?: unknown) => void;
717
error: (message: string, attr?: unknown) => void;
818
fatal: (message: string, attr?: unknown) => void;
919
debug: (message: string, attr?: unknown, level?: 1 | 2 | 3 | 4 | 5) => void;
1020
};
11-
}
1221

13-
export default function constructShellLog(messageBus: MongoshBus): ShellLog {
14-
return {
15-
log: {
22+
get _instanceState(): ShellInstanceState {
23+
return this[instanceStateSymbol];
24+
}
25+
26+
constructor(instanceState: ShellInstanceState) {
27+
super();
28+
this[instanceStateSymbol] = instanceState;
29+
this.log = {
1630
info(message: string, attr?: unknown) {
17-
messageBus.emit('mongosh:write-custom-log', {
31+
instanceState.messageBus.emit('mongosh:write-custom-log', {
1832
method: 'info',
1933
message,
2034
attr,
2135
});
2236
},
2337
warn(message: string, attr?: unknown) {
24-
messageBus.emit('mongosh:write-custom-log', {
38+
instanceState.messageBus.emit('mongosh:write-custom-log', {
2539
method: 'warn',
2640
message,
2741
attr,
2842
});
2943
},
3044
error(message: string, attr?: unknown) {
31-
messageBus.emit('mongosh:write-custom-log', {
45+
instanceState.messageBus.emit('mongosh:write-custom-log', {
3246
method: 'error',
3347
message,
3448
attr,
3549
});
3650
},
3751
fatal(message: string, attr?: unknown) {
38-
messageBus.emit('mongosh:write-custom-log', {
52+
instanceState.messageBus.emit('mongosh:write-custom-log', {
3953
method: 'fatal',
4054
message,
4155
attr,
4256
});
4357
},
4458
debug(message: string, attr?: unknown, level?: 1 | 2 | 3 | 4 | 5) {
45-
messageBus.emit('mongosh:write-custom-log', {
59+
instanceState.messageBus.emit('mongosh:write-custom-log', {
4660
method: 'debug',
4761
message,
4862
attr,
4963
level,
5064
});
5165
},
52-
},
53-
};
66+
};
67+
}
5468
}

0 commit comments

Comments
 (0)