Skip to content

Commit

Permalink
fix(init): output details and exit error code if unexpected error (#40)
Browse files Browse the repository at this point in the history
by handling `process.unhandledRejection` event
  • Loading branch information
ybiquitous committed Oct 20, 2017
1 parent bf3e0cb commit 1f8a042
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const yargs = require('yargs')
const init = require('./init')

process.on('unhandledRejection', (reason) => {
process.stderr.write(reason.stack)
process.exit(1)
})

module.exports = function cli() {
// eslint-disable-next-line no-unused-expressions
yargs
Expand Down
11 changes: 11 additions & 0 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import os from 'os'
import fs from 'fs-extra'
import assert from 'assert'
import assertThrows from './assert-throws'
import exec from './exec'

suite('init', () => {
Expand Down Expand Up @@ -104,4 +105,14 @@ suite('init', () => {
}
`)
})

test('error', async () => {
await fs.remove(packageJson)
const error = await assertThrows(() => exec('init'))
const { code, stdout, stderr } = error
assert(error instanceof Error)
assert(code === 1)
assert(stdout === '')
assert(stderr.includes('Error: ENOENT:'), stderr)
})
})

0 comments on commit 1f8a042

Please sign in to comment.