Skip to content

Commit bea0b25

Browse files
author
wenli.lw
committed
fix: fix cannot catch exception promblem
1 parent 74473de commit bea0b25

File tree

6 files changed

+27
-20
lines changed

6 files changed

+27
-20
lines changed

bin/index.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const parseArgs = require('../libs/parse-args')
55
const { join } = require('path')
66
const pkg = require(join(__dirname, '../package.json'))
77
const vm = require('./vm')
8-
require('colors')
8+
const chalk = require('chalk')
99

1010
let silent = false
1111

@@ -17,28 +17,21 @@ const start = function () {
1717
}
1818

1919
if (argv.version) {
20-
console.log(`\n${pkg.name} version: ${pkg.version}\n`.yellow)
21-
return
20+
return Promise.resolve(chalk.yellow(`\n${pkg.name} version: ${pkg.version}\n`))
2221
}
2322

2423
silent = argv.silent
25-
return vm().then(function (ret) {
24+
return vm()
25+
.then(function (ret) {
2626
log(ret)
2727
return require('../index.js')(argv, ret)
2828
})
2929
}
3030

31-
const ret = start()
32-
if (ret != null) {
33-
if (typeof ret === 'object' && ret.then) {
34-
ret.then(function (ret) {
35-
if (!silent) {
36-
console.log(ret)
37-
}
38-
}).catch(function (e) {
39-
console.error(e.red)
40-
})
41-
} else {
31+
start().then(function (ret) {
32+
if (!silent) {
4233
console.log(ret)
4334
}
44-
}
35+
}).catch(function (e) {
36+
console.error(chalk.red(e.message), '\n', e)
37+
})

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
let fire = require('./libs/index')
22

3-
global.Promise = require('bluebird') // replace default promise for debug
43
const wrap = require('./libs/wrap')
54
const log = require('./libs/util/log')
65
const parseArgs = require('./libs/parse-args')

libs/util/log.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const callerId = require('caller-id')
2+
const chalk = require('chalk')
23
module.exports = process.env.DEBUG === 'log' ? function (...args) {
34
let data = callerId.getData()
4-
console.log(`${data.filePath} > ${data.functionName}:${data.lineNumber}`.blue, '\n', ...args, '\n----------------'.blue)
5+
console.log(
6+
chalk.blue(`${data.filePath} > ${data.functionName}:${data.lineNumber}`),
7+
'\n',
8+
...args,
9+
chalk.blue('\n----------------')
10+
)
511
} : function () {}

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fire",
3-
"version": "0.2.6",
3+
"version": "0.2.7",
44
"description": "Run a js object or a function by command line directly.",
55
"bin": {
66
"fire": "./bin/index.js"
@@ -10,11 +10,14 @@
1010
"test": "standard --fix && ava",
1111
"start": "node bin/index"
1212
},
13+
"standard": {
14+
"ignore": ["test/fixtures/error.js"]
15+
},
1316
"dependencies": {
1417
"bluebird": "^3.4.7",
1518
"caller-id": "^0.1.0",
19+
"chalk": "^1.1.3",
1620
"co": "^4.6.0",
17-
"colors": "^1.1.2",
1821
"debug": "^2.6.1",
1922
"rc": "^1.1.7",
2023
"resolve": "^1.3.2",

test/fixtures/error.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
should throw error

test/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ test('exec2', async (t) => {
1414
t.is(await exec('./examples/test2.js hello name=world !'), 'hello world!')
1515
t.pass()
1616
})
17+
18+
test('should catch error', async t => {
19+
const ret = await exec('./test/fixtures/error.js')
20+
t.is(ret, '')
21+
})

0 commit comments

Comments
 (0)