Skip to content

Commit

Permalink
feat(cli): catch build errors with cli:buildError hook (#6475)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie authored and pi0 committed Sep 29, 2019
1 parent a2be217 commit 27e0353
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 17 additions & 3 deletions packages/cli/src/commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@ export default {
},

async startDev (cmd, argv) {
let nuxt
try {
const nuxt = await this._startDev(cmd, argv)
nuxt = await this._listenDev(cmd, argv)
} catch (error) {
consola.fatal(error)
return
}

return nuxt
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)

Expand All @@ -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)

Expand Down
7 changes: 4 additions & 3 deletions packages/cli/test/unit/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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'))
})

Expand Down Expand Up @@ -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)
})

Expand All @@ -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 () => {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/utils/mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const mockNuxt = (implementation) => {
}
},
options: {},
callHook: jest.fn(),
clearHook: jest.fn(),
clearHooks: jest.fn(),
close: jest.fn(),
Expand Down

0 comments on commit 27e0353

Please sign in to comment.