Skip to content

Commit 711694b

Browse files
author
wenli.lw
committed
fix: set pipe timeout
1 parent fe63b3d commit 711694b

File tree

8 files changed

+30
-2878
lines changed

8 files changed

+30
-2878
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ $ fire ./test.js arg1 arg2
1616

1717
[Example](./examples.md)
1818

19-
### As a cli
19+
20+
### Fire remote js
21+
22+
> curl https://raw.githubusercontent.com/lwdgit/node-fire/dev/examples/calc.js | fire add 1 2
23+
24+
### Fire local
2025
```
2126
//test.js
2227
module.exports = function(arg1, arg2) {
@@ -105,7 +110,7 @@ wrap(calc)(process.argv)
105110
node ./wrap.js 3 4
106111
```
107112

108-
### As a npm scripts
113+
### As a npm scripts runner
109114

110115
```
111116
//package.json

bin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const start = function () {
2323

2424
silent = argv.silent
2525
return vm().then(function (ret) {
26+
log(ret)
2627
return require('../index.js')(argv, ret)
2728
})
2829
}

bin/vm.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
1-
const { isTTY } = process.stdin
1+
const stdin = process.stdin
22

3-
const isPipe = !isTTY && 'start' in process.stdin
3+
const isPipe = !stdin.isTTY
44

55
const check = function () {
66
return new Promise(function (resolve) {
77
if (isPipe) {
88
const data = []
9-
process.stdin.on('readable', function () {
9+
// if not get readable data after 30s, give up
10+
let tick = setTimeout(function () {
11+
stdin.end && stdin.end()
12+
resolve()
13+
}, 30000)
14+
15+
stdin.on('readable', function () {
16+
clearTimeout(tick)
1017
let chunk = ''
1118
while ((chunk = this.read(), chunk)) {
1219
data.push(chunk)
1320
}
1421
})
15-
process.stdin.once('end', function () {
22+
stdin.on('end', function () {
1623
resolve(run(data.join('')))
1724
})
1825
} else {
19-
resolve(undefined)
26+
resolve()
2027
}
2128
})
2229
}

libs/util/log.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const callerId = require('caller-id')
2-
module.exports = process.env.DEBUG ? function (...args) {
2+
module.exports = process.env.DEBUG === 'log' ? function (...args) {
33
let data = callerId.getData()
44
console.log(`${data.filePath} > ${data.functionName}:${data.lineNumber}`.blue, '\n', ...args, '\n----------------'.blue)
55
} : function () {}

libs/wrap.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ function * execute (fn, args, opts) {
5959
fnLen = tmpArg.length
6060
}
6161

62-
// if (args.length < fnLen) {
63-
// concat(args, new Array(fnLen - args.length));
64-
// }
65-
6662
log('args', args)
6763

6864
log(args.slice(0, fnLen))

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fire",
3-
"version": "0.2.4",
3+
"version": "0.2.5",
44
"description": "Run a js object or a function by command line directly.",
55
"bin": {
66
"fire": "./bin/index.js"
@@ -18,7 +18,6 @@
1818
"debug": "^2.6.1",
1919
"rc": "^1.1.7",
2020
"resolve": "^1.3.2",
21-
"xargs": "^1.1.3",
2221
"yargs": "^6.6.0"
2322
},
2423
"devDependencies": {

test/pipe.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
const test = require('ava')
22
const exec = require('./_exec')
3-
test('calc', (t) => {
3+
test('pipe', (t) => {
44
t.is(exec('< examples/calc.js add 3 4'), '7')
55
t.is(exec('< examples/calc.js multiply 3 4'), '12')
66
t.is(exec('< examples/calc.js pow 3'), '9')
77
t.is(exec('< examples/calc.js div 8 0 --b 2'), '4')
88
})
9+
10+
test('pipe2', (t) => {
11+
t.is(exec('cat examples/calc.js | node ./bin/index add 3 4', ''), '7')
12+
t.is(exec('cat examples/calc.js | node ./bin/index multiply 3 4', ''), '12')
13+
t.is(exec('cat examples/calc.js | node ./bin/index pow 3', ''), '9')
14+
t.is(exec('cat examples/calc.js | node ./bin/index div 8 0 --b 2', ''), '4')
15+
})

0 commit comments

Comments
 (0)