Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(repl): shutdown providers on .exit #203

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading