Skip to content

Commit 5187a63

Browse files
committed
Add exit/quit/exit()/quit() (#220)
1 parent a09412b commit 5187a63

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import {
2-
shellApiClassDefault,
3-
hasAsyncChild,
4-
ShellApiClass, returnsPromise,
5-
} from './decorators';
1+
import { hasAsyncChild, returnsPromise, ShellApiClass, shellApiClassDefault } from './decorators';
62
import { CursorIterationResult } from './result';
73
import ShellInternalState from './shell-internal-state';
4+
import { ReplPlatform } from '@mongosh/service-provider-core';
5+
import { MongoshUnimplementedError } from '@mongosh/errors';
86

97
@shellApiClassDefault
108
@hasAsyncChild
@@ -22,6 +20,16 @@ export default class ShellApi extends ShellApiClass {
2220
show(arg): any {
2321
return this.internalState.currentDb.mongo.show(arg);
2422
}
23+
async exit(): Promise<void> {
24+
await this.internalState.close(true);
25+
if (this.internalState.initialServiceProvider.platform === ReplPlatform.CLI) {
26+
process.exit();
27+
} else {
28+
throw new MongoshUnimplementedError(
29+
`exit not supported for current platform: ${ReplPlatform[this.internalState.initialServiceProvider.platform]}`
30+
);
31+
}
32+
}
2533

2634
@returnsPromise
2735
async it(): Promise<any> {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export default class ShellInternalState {
7676
setCtx(contextObject: any): void {
7777
this.context = contextObject;
7878
contextObject.toIterator = toIterator;
79-
contextObject.print = async(arg): void => {
79+
contextObject.print = async(arg): Promise<void> => {
8080
if (arg.toReplString) {
8181
console.log(await arg.toReplString());
8282
} else {
@@ -91,6 +91,7 @@ export default class ShellInternalState {
9191
return this.shellApi[n](...args);
9292
};
9393
});
94+
contextObject.quit = contextObject.exit;
9495
contextObject.help = this.shellApi.help;
9596
contextObject.printjson = contextObject.print;
9697
Object.assign(contextObject, ShellBson);

packages/shell-evaluator/src/shell-evaluator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ class ShellEvaluator {
6868
return this.internalState.shellApi.show(argv[0]);
6969
case 'it':
7070
return this.internalState.shellApi.it();
71+
case 'exit':
72+
case 'quit':
73+
return await this.internalState.shellApi.exit();
7174
case 'enableTelemetry()':
7275
if (this.container) {
7376
return this.container.toggleTelemetry(true);

0 commit comments

Comments
 (0)