lib/src/cli/directive/ShellDirective.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
import {Directive} from '../Directive'
import * as colors from 'colors/safe'
2021-06-02 01:59:40 +00:00
import * as repl from 'repl'
2021-06-03 03:36:25 +00:00
import {DependencyKey} from '../../di'
2021-06-02 01:59:40 +00:00
/**
* Launch an interactive REPL shell from within the application.
* This is very useful for debugging and testing things during development.
*/
export class ShellDirective extends Directive {
protected options: any = {
2021-06-03 03:36:25 +00:00
welcome: `powered by Extollo, © ${(new Date()).getFullYear()} Garrett Mills\nAccess your application using the "app" global.`,
2021-06-02 01:59:40 +00:00
prompt: `${colors.blue('(')}extollo${colors.blue(') ➤ ')}`,
}
/**
* The created Node.js REPL server.
* @protected
*/
protected repl?: repl.REPLServer
getDescription(): string {
return 'launch an interactive shell inside your application'
}
getKeywords(): string | string[] {
return ['shell']
}
getHelpText(): string {
return ''
}
async handle(): Promise<void> {
const state: any = {
app: this.app(),
lib: await import('../../index'),
2021-06-02 01:59:40 +00:00
make: (target: DependencyKey, ...parameters: any[]) => this.make(target, ...parameters),
}
await new Promise<void>(res => {
2021-06-03 03:36:25 +00:00
this.nativeOutput(this.options.welcome)
2021-06-02 01:59:40 +00:00
this.repl = repl.start(this.options.prompt)
Object.assign(this.repl.context, state)
this.repl.on('exit', () => res())
})
}
}