From 27e03531d6602a35b2df2e988b65f2224b8759b4 Mon Sep 17 00:00:00 2001 From: Pim Date: Sun, 29 Sep 2019 10:11:32 +0200 Subject: [PATCH] feat(cli): catch build errors with `cli:buildError` hook (#6475) --- packages/cli/src/commands/dev.js | 22 ++++++++++++++++++---- packages/cli/test/unit/dev.test.js | 7 ++++--- packages/cli/test/utils/mocking.js | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/commands/dev.js b/packages/cli/src/commands/dev.js index c2047ab909..4aa5f7df3c 100644 --- a/packages/cli/src/commands/dev.js +++ b/packages/cli/src/commands/dev.js @@ -27,16 +27,25 @@ export default { }, async startDev (cmd, argv) { + let nuxt try { - const nuxt = await this._startDev(cmd, argv) - - return nuxt + nuxt = await this._listenDev(cmd, argv) } catch (error) { + consola.fatal(error) + return + } + + try { + await this._buildDev(cmd, argv, nuxt) + } catch (error) { + await nuxt.callHook('cli:buildError', error) consola.error(error) } + + return nuxt }, - async _startDev (cmd, argv) { + async _listenDev (cmd, argv) { const config = await cmd.getNuxtConfig({ dev: true, _build: true }) const nuxt = await cmd.getNuxt(config) @@ -60,6 +69,11 @@ export default { await Promise.all(openerPromises) } + // Return instance + return nuxt + }, + + async _buildDev (cmd, argv, nuxt) { // Create builder instance const builder = await cmd.getBuilder(nuxt) diff --git a/packages/cli/test/unit/dev.test.js b/packages/cli/test/unit/dev.test.js index e0b135160a..2654b4ea03 100644 --- a/packages/cli/test/unit/dev.test.js +++ b/packages/cli/test/unit/dev.test.js @@ -41,7 +41,7 @@ describe('dev', () => { expect(consola.error).not.toHaveBeenCalled() }) - test('catches build error', async () => { + test('catches build error and calls hook', async () => { const Nuxt = mockNuxt() const Builder = mockBuilder() @@ -55,6 +55,7 @@ describe('dev', () => { await Nuxt.fileRestartHook(builder) expect(Nuxt.prototype.close).toHaveBeenCalled() + expect(Nuxt.prototype.callHook).toHaveBeenCalledWith('cli:buildError', expect.any(Error)) expect(consola.error).toHaveBeenCalledWith(new Error('Build Error')) }) @@ -88,7 +89,7 @@ describe('dev', () => { builder.nuxt = new Nuxt() await Nuxt.fileRestartHook(builder) - expect(consola.error).toHaveBeenCalledWith(new Error('Config Error')) + expect(consola.fatal).toHaveBeenCalledWith(new Error('Config Error')) // expect(Builder.prototype.watchRestart).toHaveBeenCalledTimes(1) }) @@ -104,7 +105,7 @@ describe('dev', () => { await NuxtCommand.from(dev).run() - expect(consola.error).toHaveBeenCalledWith(new Error('Listen Error')) + expect(consola.fatal).toHaveBeenCalledWith(new Error('Listen Error')) }) test('dev doesnt force-exit by default', async () => { diff --git a/packages/cli/test/utils/mocking.js b/packages/cli/test/utils/mocking.js index dc5edaef23..045a4e6e65 100644 --- a/packages/cli/test/utils/mocking.js +++ b/packages/cli/test/utils/mocking.js @@ -83,6 +83,7 @@ export const mockNuxt = (implementation) => { } }, options: {}, + callHook: jest.fn(), clearHook: jest.fn(), clearHooks: jest.fn(), close: jest.fn(),