diff --git a/src/cli.js b/src/cli.js index 0446314..54f58ec 100644 --- a/src/cli.js +++ b/src/cli.js @@ -1,10 +1,7 @@ -const {parse, parseCommand, tokensToCommand} = require('./parser') -const {Manager} = require('./manager') +const {parse, parseCommand, tokensToCommand, extractValue} = require('./parser') +const {Manager, EVENTS} = require('./manager') const readline = require('readline') const EventEmmiter = require('events') -const EVENTS = { - commandExecuted: 'commandExecuted' -} const commands = [ // List commands. @@ -110,7 +107,15 @@ function runCommand(_commands, injection = null, tokens) { injection = _injection } const manager = new Manager(allCommands, injection) - const request = tokensToCommand(tokens) + manager.events.on(EVENTS.error, ({e, request}) => { + const { console } = injection + console.log(e.toString()) + const stack = extractValue(request.args, 'stack', false) + if (stack) { + console.log(e.stack) + } + }) + request = tokensToCommand(tokens) return manager.execute(request) } diff --git a/src/manager.js b/src/manager.js index 13f39de..60efc49 100644 --- a/src/manager.js +++ b/src/manager.js @@ -1,6 +1,7 @@ const EventEmitter = require('events') const EVENTS = { - onError: 'onError' + error: 'error', + executed: 'executed' } class Manager { @@ -10,13 +11,16 @@ class Manager { this.events = new EventEmitter() } - execute(request) { + async execute(request) { const command = this.get(request) try { - return command.handler({command, request, injection: this.injection, manager: this}) + const result = await command.handler({command, request, injection: this.injection, manager: this}) + this.events.emit(EVENTS.executed, { request, result }) + + return result } catch(e) { - this.events.emit(EVENTS.onError, e) + this.events.emit(EVENTS.error, {e, request}) } } diff --git a/test/cli.spec.js b/test/cli.spec.js index 66dca5e..1f4a708 100644 --- a/test/cli.spec.js +++ b/test/cli.spec.js @@ -16,8 +16,6 @@ describe('Commander', () => { title: 'Prints values', handler: async ({ injection: { console } }) => { console.log('Run commander testing.') - - return true } }, ], @@ -29,6 +27,41 @@ describe('Commander', () => { const line = _console.objects.pop() assert.equal(line, 'Run commander testing.') }) + + describe('Handle error message', async () => { + const runWithError = async (tokens) => { + await runCommand( + [ + { + name: 'print', + title: 'Prints values', + handler: async ({ injection: { console } }) => { + throw new Error('Printer does not work.') + } + }, + ], + { + console: _console + }, + tokens + ) + } + + it('Error', async () => { + await runWithError(['print']) + const line = _console.objects.pop() + const etalon = 'Error: Printer does not work.' + assert.equal(line, etalon) + }) + + it('Error with stack', async () => { + await runWithError(['print', '--stack']) + const lines = _console.objects.pop().split('\n') + const etalon = 'Error: Printer does not work.' + assert.equal(lines[0], etalon) + assert.notEqual(lines[6].match(/at callFn/i), null) + }) + }) }) describe('CLI', () => { diff --git a/test/manager.spec.js b/test/manager.spec.js index 033a5b4..965ee53 100644 --- a/test/manager.spec.js +++ b/test/manager.spec.js @@ -1,7 +1,7 @@ const assert = require('assert') const { Manager, EVENTS } = require('../src/manager') -describe('CLI commander testing', () => { +describe('Command manager testing', () => { let manager const injection = { time: 'timeString' @@ -62,15 +62,29 @@ describe('CLI commander testing', () => { assert.equal(command.group, 'default') }) - it('Error invoking command.', (done) => { - const request = { - name: 'invokeError', - } - manager.events.once(EVENTS.onError, (e) => { - assert.equal('Test error.', e.message) - done() + describe('Events', () => { + it('Error invoking command.', (done) => { + const request = { + name: 'invokeError', + } + manager.events.once(EVENTS.error, ({e, request: _request}) => { + assert.equal('Test error.', e.message) + assert.equal(request.name, _request.name) + done() + }) + manager.execute(request) + }) + + it('Executed', (done) => { + const request = { + name: 'command', + } + manager.events.once(EVENTS.executed, ({request: _request}) => { + assert.equal(request.name, _request.name) + done() + }) + manager.execute(request) }) - manager.execute(request) }) it('Get', () => {