feat(generate): return non-zero code or page error (fixes #4991) (#5195)

This commit is contained in:
Jarek Lipski 2019-03-11 20:55:54 +01:00 committed by Pooya Parsa
parent 1336ac9c56
commit c6565c97c6
3 changed files with 37 additions and 1 deletions

View File

@ -33,6 +33,11 @@ export default {
options.modern = 'client' options.modern = 'client'
} }
} }
},
'fail-on-error': {
type: 'boolean',
default: false,
description: 'Exit with non-zero status code if there are errors when generating pages'
} }
}, },
async run(cmd) { async run(cmd) {
@ -66,9 +71,13 @@ export default {
const generator = await cmd.getGenerator(nuxt) const generator = await cmd.getGenerator(nuxt)
await generator.generate({ const { errors } = await generator.generate({
init: true, init: true,
build: cmd.argv.build build: cmd.argv.build
}) })
if (cmd.argv['fail-on-error'] && errors.length > 0) {
throw new Error('Error generating pages, exiting with non-zero code')
}
} }
} }

View File

@ -113,6 +113,7 @@ describe('generate', () => {
mockGetGenerator(async () => { mockGetGenerator(async () => {
await buildDone() await buildDone()
return { errors: [] }
}) })
const cmd = NuxtCommand.from(generate, ['generate', '.']) const cmd = NuxtCommand.from(generate, ['generate', '.'])
@ -134,4 +135,28 @@ describe('generate', () => {
expect(createLock).not.toHaveBeenCalled() expect(createLock).not.toHaveBeenCalled()
}) })
test('throw an error when fail-on-error enabled and page errors', async () => {
mockGetNuxt()
mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] }))
const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error'])
await expect(cmd.run()).rejects
})
test('do not throw an error when fail-on-error disabled and page errors', async () => {
mockGetNuxt()
mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] }))
const cmd = NuxtCommand.from(generate, ['generate', '.'])
await cmd.run()
})
test('do not throw an error when fail-on-error enabled and no page errors', async () => {
mockGetNuxt()
mockGetGenerator()
const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error'])
await cmd.run()
})
}) })

View File

@ -39,6 +39,8 @@ export const mockGetGenerator = (ret) => {
const generate = jest.fn() const generate = jest.fn()
if (ret) { if (ret) {
generate.mockImplementationOnce(ret) generate.mockImplementationOnce(ret)
} else {
generate.mockImplementationOnce(() => ({ errors: [] }))
} }
Command.prototype.getGenerator = jest.fn().mockImplementationOnce(() => ({ generate })) Command.prototype.getGenerator = jest.fn().mockImplementationOnce(() => ({ generate }))