Skip to content

Commit

Permalink
Merge pull request #203 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(repl): shutdown providers on .exit
  • Loading branch information
jlenon7 authored Jan 6, 2024
2 parents a971a7a + e21739f commit 4c950bf
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
3 changes: 2 additions & 1 deletion src/applications/Repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 14 additions & 1 deletion src/repl/ReplImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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.
*/
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/applications/ReplTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand All @@ -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()

Expand Down

0 comments on commit 4c950bf

Please sign in to comment.