From e21739fcba26109f3c20fd26aa96b0421d32c844 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Sat, 6 Jan 2024 19:51:15 +0000 Subject: [PATCH] feat(repl): shutdown providers on .exit --- package-lock.json | 4 ++-- package.json | 2 +- src/applications/Repl.ts | 3 ++- src/repl/ReplImpl.ts | 15 ++++++++++++++- tests/unit/applications/ReplTest.ts | 5 ++++- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ce8447..a0be9cd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/core", - "version": "4.17.0", + "version": "4.18.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/core", - "version": "4.17.0", + "version": "4.18.0", "license": "MIT", "dependencies": { "pretty-repl": "^3.1.2", diff --git a/package.json b/package.json index 1fd2021..3e2081e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "4.17.0", + "version": "4.18.0", "description": "The plug and play Node.js framework.", "license": "MIT", "author": "João Lenon ", diff --git a/src/applications/Repl.ts b/src/applications/Repl.ts index 09588ca..b79310d 100644 --- a/src/applications/Repl.ts +++ b/src/applications/Repl.ts @@ -36,12 +36,13 @@ export class Repl { Logger.gray("const { User } = await import('#app/models/User')\n") Logger.write( - `${Color.yellow.bold('To see all commands available type:')} .help\n\n` + `${Color.yellow.bold('To see all commands available type:')} .help\n` ) repl .setPrompt(Color.purple.bold('Athenna ') + Color.green.bold('❯ ')) .displayPrompt(false) + .shutdownProviders() .commandImpl(Ls) .commandImpl(Clean) diff --git a/src/repl/ReplImpl.ts b/src/repl/ReplImpl.ts index 245b1fc..6d17876 100644 --- a/src/repl/ReplImpl.ts +++ b/src/repl/ReplImpl.ts @@ -7,9 +7,10 @@ * file that was distributed with this source code. */ -import { Is, Module, Options, String } from '@athenna/common' +import { LoadHelper } from '#src/helpers/LoadHelper' import type { Command } from '#src/repl/helpers/Command' import type { REPLServer, ReplOptions } from 'node:repl' +import { Is, Module, Options, String } from '@athenna/common' import { CommandBuilder } from '#src/repl/helpers/CommandBuilder' type REPLWriteKey = { @@ -41,6 +42,18 @@ export class ReplImpl { return this.session } + /** + * Shutdown all Athenna providers on exit. + */ + public shutdownProviders() { + this.session?.on('exit', async () => { + await LoadHelper.shutdownProviders() + process.exit(0) + }) + + return this + } + /** * Define a command in the repl session. */ diff --git a/tests/unit/applications/ReplTest.ts b/tests/unit/applications/ReplTest.ts index 87df090..4e2ec14 100644 --- a/tests/unit/applications/ReplTest.ts +++ b/tests/unit/applications/ReplTest.ts @@ -57,13 +57,14 @@ export default class ReplTest { Repl.when('setPrompt').return(Repl) Repl.when('displayPrompt').return(Repl) Repl.when('commandImpl').return(Repl) + Repl.when('shutdownProviders').return(Repl) await ReplApp.boot() assert.calledWith(process.stdout.write, chalkRainbow(figlet.textSync('REPL\n')) + '\n') assert.calledWith(process.stdout.write, Color.gray('To import your modules use dynamic imports:\n') + '\n') assert.calledWith(process.stdout.write, Color.gray("const { User } = await import('#app/models/User')\n") + '\n') - assert.calledWith(process.stdout.write, Color.yellow.bold('To see all commands available type:') + ' .help\n\n\n') + assert.calledWith(process.stdout.write, Color.yellow.bold('To see all commands available type:') + ' .help\n\n') } @Test() @@ -74,6 +75,7 @@ export default class ReplTest { Repl.when('setPrompt').return(Repl) Repl.when('displayPrompt').return(Repl) Repl.when('commandImpl').return(Repl) + Repl.when('shutdownProviders').return(Repl) await ReplApp.boot() @@ -89,6 +91,7 @@ export default class ReplTest { Repl.when('setPrompt').return(Repl) Repl.when('displayPrompt').return(Repl) Repl.when('commandImpl').return(Repl) + Repl.when('shutdownProviders').return(Repl) await ReplApp.boot()