[Pkg-javascript-commits] [node-foreground-child] 13/69: slice arguments, added documentation

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 11:43:02 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-foreground-child.

commit 86fd2b04d975a7835e806e27b3ea2cbd328efc54
Author: Benjamin Coe <ben at npmjs.com>
Date:   Sat Jun 20 23:43:49 2015 -0400

    slice arguments, added documentation
---
 README.md     | 12 +++++++++++-
 index.js      | 14 ++++++++++++--
 test/basic.js |  4 +++-
 3 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/README.md b/README.md
index e980df2..73ae9cc 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ child processes for test coverage and such.
 
 ## USAGE
 
-```
+```js
 var foreground = require('foreground-child')
 
 // cats out this file
@@ -21,3 +21,13 @@ var child = foreground('cat', [__filename])
 // If the child gets a signal, or just exits, then this
 // parent process will exit in the same way.
 ```
+
+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()
+})
+```
diff --git a/index.js b/index.js
index dd50b0e..c0e4793 100644
--- a/index.js
+++ b/index.js
@@ -2,10 +2,18 @@ var signalExit = require('signal-exit')
 var spawn = require('child_process').spawn
 
 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) {
@@ -16,9 +24,11 @@ module.exports = function (program, args, cb) {
     args = program.slice(1)
     program = program[0]
   } else if (!Array.isArray(args)) {
-    args = [].slice.call(arguments, 1)
+    args = [].slice.call(arguments, 1, arrayIndex)
   }
 
+  var c
+
   var child = spawn(program, args, { stdio: 'inherit' })
 
   var childExited = false
@@ -38,7 +48,7 @@ module.exports = function (program, args, cb) {
         setTimeout(function () {}, 200)
         process.kill(process.pid, signal)
       } else
-        process.exit(code)      
+        process.exit(code)
     })
   })
 
diff --git a/test/basic.js b/test/basic.js
index 96ffbb6..0f67cbe 100644
--- a/test/basic.js
+++ b/test/basic.js
@@ -103,6 +103,8 @@ t.test('exit codes', function (t) {
 })
 
 t.test('parent emits exit when SIGTERMed', function (t) {
+  if (isZero10OnTravis()) return t.done()
+
   var which = ['parent', 'child', 'nobody']
   which.forEach(function (who) {
     t.test('SIGTERM ' + who, function (t) {
@@ -112,7 +114,7 @@ t.test('parent emits exit when SIGTERMed', function (t) {
       var out = ''
       child.stdout.on('data', function (c) { out += c })
       child.on('close', function (code, signal) {
-        if (who === 'nobody' || (isZero10OnTravis() && who === 'child'))
+        if (who === 'nobody')
           t.equal(signal, null)
         else
           t.equal(signal, 'SIGTERM')

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-foreground-child.git



More information about the Pkg-javascript-commits mailing list