diff --git a/packages/cli/src/command.js b/packages/cli/src/command.js index 7108d0450f..296cbbca09 100644 --- a/packages/cli/src/command.js +++ b/packages/cli/src/command.js @@ -31,33 +31,47 @@ export default class NuxtCommand { return new NuxtCommand(cmd, argv) } - run() { + async run() { if (this.argv.help) { this.showHelp() - return Promise.resolve() + return } if (this.argv.version) { this.showVersion() - return Promise.resolve() + return } if (typeof this.cmd.run !== 'function') { - return Promise.resolve() + return } - const runResolve = Promise.resolve(this.cmd.run(this)) + let cmdError + + try { + await this.cmd.run(this) + } catch (e) { + cmdError = e + } if (this.argv.lock) { - runResolve.then(() => this.releaseLock()) + await this.releaseLock() } if (this.argv['force-exit']) { const forceExitByUser = this.isUserSuppliedArg('force-exit') - runResolve.then(() => forceExit(this.cmd.name, forceExitByUser ? false : forceExitTimeout)) + if (cmdError) { + consola.fatal(cmdError) + } + forceExit(this.cmd.name, forceExitByUser ? false : forceExitTimeout) + if (forceExitByUser) { + return + } } - return runResolve + if (cmdError) { + throw cmdError + } } showVersion() { diff --git a/packages/cli/test/unit/generate.test.js b/packages/cli/test/unit/generate.test.js index 8905284def..44b54cc9f4 100644 --- a/packages/cli/test/unit/generate.test.js +++ b/packages/cli/test/unit/generate.test.js @@ -141,7 +141,7 @@ describe('generate', () => { mockGetGenerator(() => ({ errors: [{ type: 'dummy' }] })) const cmd = NuxtCommand.from(generate, ['generate', '.', '--fail-on-error']) - await expect(cmd.run()).rejects + await expect(cmd.run()).rejects.toThrow('Error generating pages, exiting with non-zero code') }) test('do not throw an error when fail-on-error disabled and page errors', async () => { diff --git a/test/utils/setup.js b/test/utils/setup.js index bb74d136c9..cda2252ad1 100644 --- a/test/utils/setup.js +++ b/test/utils/setup.js @@ -1,6 +1,7 @@ import consola from 'consola' import chalk from 'chalk' import env from 'std-env' +import exit from 'exit' const isWin = env.windows @@ -15,3 +16,11 @@ chalk.enabled = false jest.setTimeout(60000) consola.mockTypes(() => jest.fn()) + +function errorTrap(error) { + process.stderr.write('\n' + error.stack + '\n') + exit(1) +} + +process.on('unhandledRejection', errorTrap) +process.on('uncaughtException', errorTrap)