Skip to content

Commit 74473de

Browse files
author
wenli.lw
committed
add isPipe
1 parent 47c96c3 commit 74473de

File tree

14 files changed

+73
-57
lines changed

14 files changed

+73
-57
lines changed

bin/vm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const stdin = process.stdin
22

3-
const isPipe = !stdin.isTTY
3+
const isPipe = process.env.isPipe == null ? !stdin.isTTY : process.env.isPipe === 'true'
44

55
const check = function () {
66
return new Promise(function (resolve) {

examples.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
## Known support list
2323
24-
| node modules | example |
24+
| node_modules | example |
2525
| --- | --- |
2626
| [shelljs](https://github.com/shelljs/shelljs) | fire shelljs ls stdout |
2727
| [mathjs](https://github.com/josdejong/mathjs) | fire mathjs add 2.1 3.2 |
@@ -32,5 +32,6 @@
3232
| md5 | fire md5 test |
3333
| sha1 | fire sha1 test |
3434
| uuid | fire uuid |
35-
35+
| colors | fire colors |
36+
| chalk | fire chalk |
3637

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fire",
3-
"version": "0.2.5",
3+
"version": "0.2.6",
44
"description": "Run a js object or a function by command line directly.",
55
"bin": {
66
"fire": "./bin/index.js"

test/_exec.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
const exec = require('shelljs').exec
2-
module.exports = function (script, bin = 'node ./bin/index') {
3-
console.log('RUN>', bin + ' ' + script)
4-
return exec(bin + ' ' + script).toString().trim()
1+
const exec = function (args, isPipe) {
2+
return new Promise(function (resolve) {
3+
console.log('RUN >> ', args)
4+
var proc = require('child_process').exec(args, {
5+
env: Object.assign({}, process.env, { isPipe }
6+
)}, (err, ret) => resolve(String(err || ret || '').trim()))
7+
8+
proc.stdout.on('data', data => {
9+
process.stdout.write(data)
10+
})
11+
12+
proc.stderr.on('data', err => {
13+
process.stdout.write(err)
14+
})
15+
})
16+
}
17+
18+
module.exports = function (script, bin = 'node ./bin/index', isPipe = false) {
19+
return exec(bin + ' ' + script, isPipe)
520
}

test/calc.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const test = require('ava')
22
const exec = require('./_exec')
3-
test('calc', (t) => {
4-
t.is(exec('examples/calc.js add 3 4'), '7')
5-
t.is(exec('examples/calc.js multiply 3 4'), '12')
6-
t.is(exec('examples/calc.js pow 3'), '9')
7-
t.is(exec('examples/calc.js div 8 0 --b 2'), '4')
3+
test('calc', async (t) => {
4+
t.is(await exec('examples/calc.js add 3 4'), '7')
5+
t.is(await exec('examples/calc.js multiply 3 4'), '12')
6+
t.is(await exec('examples/calc.js pow 3'), '9')
7+
t.is(await exec('examples/calc.js div 8 0 --b 2'), '4')
88
})

test/force-input.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const test = require('ava')
22
const exec = require('./_exec')
3-
test('force input', (t) => {
4-
let ret = exec('./test/fixtures/dynamicArgs.js ...2,4')
3+
test('force input', async (t) => {
4+
let ret = await exec('./test/fixtures/dynamicArgs.js ...2,4')
55
console.log(ret)
66
t.is('6', ret)
77
})

test/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
const test = require('ava')
22
const exec = require('./_exec')
3-
test('exec', (t) => {
4-
exec('./test/fixtures/test.js')
3+
test('exec', async (t) => {
4+
await exec('./test/fixtures/test.js')
55
t.pass()
66
})
77

8-
test('relative path', (t) => {
9-
exec('test/fixtures/test.js')
8+
test('relative path', async (t) => {
9+
await exec('test/fixtures/test.js')
1010
t.pass()
1111
})
1212

13-
test('json & multi params', (t) => {
14-
t.is(exec('./test/fixtures/test.json repository type'), 'git')
13+
test('json & multi params', async (t) => {
14+
t.is(await exec('./test/fixtures/test.json repository type'), 'git')
1515
t.pass()
1616
})
1717

18-
test('test2', (t) => {
19-
t.is(exec('./test/fixtures/test2.js'), `{ a: [Function],
18+
test('test2', async (t) => {
19+
t.is(await exec('./test/fixtures/test2.js'), `{ a: [Function],
2020
b: [Function],
2121
c: [Function],
2222
d: [Function],
2323
e: [Function] }`)
2424
})
2525

26-
test('test2 with second param', (t) => {
27-
exec('./test/fixtures/test2.js a')
26+
test('test2 with second param', async (t) => {
27+
await exec('./test/fixtures/test2.js a')
2828
})
2929

30-
test('deep get', t => {
31-
t.is(exec('./test/fixtures/deep.js b d'), 'delay deep')
30+
test('deep get', async (t) => {
31+
t.is(await exec('./test/fixtures/deep.js b d'), 'delay deep')
3232
t.pass()
3333
})

test/input-object.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const test = require('ava')
22
const exec = require('./_exec')
3-
test('calc', (t) => {
4-
let ret = exec('./test/fixtures/dynamicArgs.js ...2,4')
3+
test('calc', async (t) => {
4+
let ret = await exec('./test/fixtures/dynamicArgs.js ...2,4')
55
console.log(ret)
66
t.is('6', ret)
77
})

test/node-module.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
const test = require('ava')
22
const exec = require('./_exec')
3-
test('run shelljs', (t) => {
3+
test('run shelljs', async (t) => {
44
let filename = '0000testfile' + Date.now()
5-
exec('shelljs touch ...' + filename)
6-
t.regex(exec('shelljs ls'), new RegExp(filename))
7-
t.is(exec('shelljs ls ..../ 0'), filename)
8-
exec('shelljs rm ...' + filename)
5+
await exec('shelljs touch ...' + filename)
6+
t.regex(await exec('shelljs ls'), new RegExp(filename))
7+
t.is(await exec('shelljs ls ..../ 0'), filename)
8+
await exec('shelljs rm ...' + filename)
99
t.pass()
1010
})
1111

12-
test('run mathjs', (t) => {
13-
t.is(exec('mathjs add 5.3 4.2'), '9.5')
12+
test('run mathjs', async (t) => {
13+
t.is(await exec('mathjs add 5.3 4.2'), '9.5')
1414
t.pass()
1515
})

test/npm-scripts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const test = require('ava')
22
const exec = require('./_exec')
33
const testDir = 'quick_test' + Math.random()
44

5-
test('test2 with second param', (t) => {
6-
let ret = exec(command, '')
5+
test('npm scripts', async (t) => {
6+
let ret = await exec(command, '')
77
t.regex(ret, new RegExp(testDir))
8-
ret = exec(cleanCommand, '')
8+
ret = await exec(cleanCommand, '')
99
t.regex(ret, new RegExp('^((?!' + testDir + ')[\\w\\W])+$'), 'rm failed, you may delete it manualy')
1010
t.pass()
1111
})

0 commit comments

Comments
 (0)