UNPKG

@whook/whook

Version:

Build strong and efficient REST web services.

73 lines (72 loc) 2.58 kB
import { autoProvider, name, location, } from 'knifecycle'; import { identity, noop } from '../libs/utils.js'; import repl from 'node:repl'; import { YError } from 'yerror'; const REPL_BANNER = ` _ ____ __ ___ _______ __ | | /| / / / ___ ___ / /__ / _ \\/ __/ _ \\/ / | |/ |/ / _ \\/ _ \\/ _ \\/ '_/ / , _/ _// ___/ /__ |__/|__/_//_/\\___/\\___/_/\\_\\ /_/|_/___/_/ /____/ Inject services with \`.inject\`. > .inject log > log('info', '👋 - Hello REPL!'); `; async function initREPL({ $ready, $instance, $injector, $dispose, log = noop, stdin = process.stdin, stdout = process.stdout, }) { log('debug', '🖵 - Initializing the REPL service!'); const replPromise = $ready.then(async () => { // Wait the event loop to be empty before // prompting for commands await Promise.resolve(); stdout.write(REPL_BANNER); const loop = repl.start({ prompt: 'whook> ', ignoreUndefined: true, breakEvalOnSigint: true, input: stdin, output: stdout, }); loop.defineCommand('registered', { help: 'List registered services.', async action() { this.clearBufferedCommand(); stdout.write(`# Registered Services:${$instance .registered() .map((name) => ` - ${name}${name in loop.context ? ' (I)' : ''}`) .join(',')}. (I: instantiated) `); this.displayPrompt(); }, }); loop.defineCommand('inject', { help: 'Inject services.', async action(name) { this.clearBufferedCommand(); const services = await $injector(name.split(/\s/g).filter(identity)); Object.keys(services).forEach((key) => { loop.context[key] = services[key]; }); this.displayPrompt(); }, }); loop.on('exit', async () => { await $dispose(); }); return loop; }); return { service: undefined, fatalErrorPromise: replPromise .catch((err) => { throw YError.wrap(err, 'E_REPL_ERROR'); }) .then(noop), dispose: async () => { const loop = await replPromise; loop.close(); }, }; } export default location(name('repl', autoProvider(initREPL)), import.meta.url); //# sourceMappingURL=repl.js.map