foreground-child
Advanced tools
Comparing version 1.2.0 to 1.3.0
46
index.js
var signalExit = require('signal-exit') | ||
var spawn = require('child_process').spawn | ||
module.exports = function (program, args) { | ||
module.exports = function (program, args, cb) { | ||
var arrayIndex = arguments.length | ||
if (typeof args === 'function') { | ||
cb = args | ||
args = undefined | ||
} else { | ||
cb = Array.prototype.slice.call(arguments).filter(function (arg, i) { | ||
if (typeof arg === 'function') { | ||
arrayIndex = i | ||
return true | ||
} | ||
})[0] | ||
} | ||
cb = cb || function (done) { | ||
return done() | ||
} | ||
if (Array.isArray(program)) { | ||
@@ -9,3 +27,3 @@ args = program.slice(1) | ||
} else if (!Array.isArray(args)) { | ||
args = [].slice.call(arguments, 1) | ||
args = [].slice.call(arguments, 1, arrayIndex) | ||
} | ||
@@ -21,13 +39,15 @@ | ||
child.on('close', function (code, signal) { | ||
childExited = true | ||
if (signal) { | ||
// If there is nothing else keeping the event loop alive, | ||
// then there's a race between a graceful exit and getting | ||
// the signal to this process. Put this timeout here to | ||
// make sure we're still alive to get the signal, and thus | ||
// exit with the intended signal code. | ||
setTimeout(function () {}, 200) | ||
process.kill(process.pid, signal) | ||
} else | ||
process.exit(code) | ||
cb(function () { | ||
childExited = true | ||
if (signal) { | ||
// If there is nothing else keeping the event loop alive, | ||
// then there's a race between a graceful exit and getting | ||
// the signal to this process. Put this timeout here to | ||
// make sure we're still alive to get the signal, and thus | ||
// exit with the intended signal code. | ||
setTimeout(function () {}, 200) | ||
process.kill(process.pid, signal) | ||
} else | ||
process.exit(code) | ||
}) | ||
}) | ||
@@ -34,0 +54,0 @@ |
{ | ||
"name": "foreground-child", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Run a child as if it's the foreground process. Give it stdio. Exit when it exits.", | ||
@@ -13,6 +13,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tap": "^1.0.4" | ||
"tap": "^1.2.1" | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"test": "tap --coverage test/*.js" | ||
}, | ||
@@ -19,0 +19,0 @@ "repository": { |
# foreground-child | ||
[![Build Status](https://travis-ci.org/isaacs/foreground-child.png)](https://travis-ci.org/isaacs/foreground-child) | ||
Run a child as if it's the foreground process. Give it stdio. Exit | ||
@@ -11,3 +13,3 @@ when it exits. | ||
``` | ||
```js | ||
var foreground = require('foreground-child') | ||
@@ -23,1 +25,11 @@ | ||
``` | ||
A callback can optionally be provided, if you want to perform an action | ||
before your foreground-child exits: | ||
```js | ||
var child = foreground('cat', [__filename], function (done) { | ||
// perform an action. | ||
return done() | ||
}) | ||
``` |
@@ -25,5 +25,16 @@ var fg = require('../index.js') | ||
if (process.argv[2] === 'parent') { | ||
var cb = undefined | ||
// we can optionally assign a beforeExit handler | ||
// to the foreground-child process; we should test it. | ||
if (process.argv[4] === 'beforeExitHandler') { | ||
cb = function (done) { | ||
console.log('beforeExitHandler') | ||
return done() | ||
} | ||
} | ||
var program = process.execPath | ||
var args = [__filename, 'child'].concat(process.argv.slice(3)) | ||
var child = fg(program, args) | ||
var child = fg(program, args, cb) | ||
@@ -46,2 +57,3 @@ if (process.argv[3] === 'signalexit') { | ||
} | ||
return | ||
@@ -95,3 +107,3 @@ } | ||
t.test('parent emits exit when SIGTERMed', function (t) { | ||
t.test('parent emits exit when SIGTERMed', { skip: isZero10OnTravis() }, function (t) { | ||
var which = ['parent', 'child', 'nobody'] | ||
@@ -117,1 +129,26 @@ which.forEach(function (who) { | ||
}) | ||
t.test('beforeExitHandler', function (t) { | ||
var codes = [0, 1, 2] | ||
codes.forEach(function (c) { | ||
t.test(c, function (t) { | ||
t.plan(3) | ||
var prog = process.execPath | ||
var args = [__filename, 'parent', c, 'beforeExitHandler'] | ||
var child = spawn(prog, args) | ||
var out = '' | ||
child.stdout.on('data', function (c) { out += c }) | ||
child.on('close', function (code, signal) { | ||
t.equal(signal, null) | ||
t.equal(code, c) | ||
t.equal(out, 'stdout\nbeforeExitHandler\n') | ||
}) | ||
}) | ||
}) | ||
t.end() | ||
}) | ||
function isZero10OnTravis () { | ||
return process.env.TRAVIS && /^v0\.10\.[0-9]+$/.test(process.version) ? | ||
'skip on 0.10 on Travis' : false | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
49143
16
566
34
1
3