[Pkg-javascript-commits] [node-foreground-child] 01/69: first

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 11:43:01 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 9dc371a8b552b123255a2c2cf89eeff6db738f81
Author: isaacs <i at izs.me>
Date:   Thu May 14 23:22:32 2015 -0700

    first
---
 LICENSE       | 15 ++++++++++++
 README.md     | 21 ++++++++++++++++
 index.js      | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 package.json  | 26 ++++++++++++++++++++
 test/basic.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 212 insertions(+)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..19129e3
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,15 @@
+The ISC License
+
+Copyright (c) Isaac Z. Schlueter and Contributors
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..72be5ea
--- /dev/null
+++ b/README.md
@@ -0,0 +1,21 @@
+# foreground-child
+
+Run a child as if it's the foreground process.  Give it stdio.  Exit
+when it exits.
+
+Mostly this module is here to support some use cases around wrapping
+child processes for test coverage and such.
+
+## USAGE
+
+```
+var foreground = require('foreground-child')
+
+// cats out this file
+var child = foreground('cat', [__filename])
+
+// At this point, it's best to just do nothing else.
+// return or whatever.
+// If the child gets a signal, or just exits, then this
+// parent process will exit in the same way.
+```
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..275d44a
--- /dev/null
+++ b/index.js
@@ -0,0 +1,78 @@
+module.exports = function (program, args) {
+  if (Array.isArray(program)) {
+    args = program.slice(1)
+    program = program[0]
+  } else if (!Array.isArray(args)) {
+    args = [].slice.call(arguments, 1)
+  }
+
+  var child = require('child_process').spawn(
+    program,
+    args,
+    { stdio: 'inherit' }
+  )
+
+  signals.forEach(function (sig) {
+    try {
+      process.on(sig, function () {
+        child.kill(sig)
+      })
+    } catch (er) {}
+  })
+
+  process.on('exit', function (code) {
+    child.kill('SIGHUP')
+  })
+
+  child.on('close', function (code, signal) {
+    if (signal) {
+      process.removeAllListeners(signal)
+      process.kill(process.pid, signal)
+    } else {
+      process.exit(code)
+    }
+  })
+
+  return child
+}
+
+var signals = [
+  'SIGABRT',
+  'SIGALRM',
+  'SIGBUS',
+  'SIGCHLD',
+  'SIGCLD',
+  'SIGCONT',
+  'SIGEMT',
+  'SIGFPE',
+  'SIGHUP',
+  'SIGILL',
+  'SIGINFO',
+  'SIGINT',
+  'SIGIO',
+  'SIGIOT',
+  'SIGKILL',
+  'SIGLOST',
+  'SIGPIPE',
+  'SIGPOLL',
+  'SIGPROF',
+  'SIGPWR',
+  'SIGQUIT',
+  'SIGSEGV',
+  'SIGSTKFLT',
+  'SIGSTOP',
+  'SIGSYS',
+  'SIGTERM',
+  'SIGTRAP',
+  'SIGTSTP',
+  'SIGTTIN',
+  'SIGTTOU',
+  'SIGUNUSED',
+  'SIGURG',
+  'SIGUSR1',
+  'SIGUSR2',
+  'SIGVTALRM',
+  'SIGWINCH',
+  'SIGXCPU',
+  'SIGXFSZ'
+]
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..1e822a5
--- /dev/null
+++ b/package.json
@@ -0,0 +1,26 @@
+{
+  "name": "foreground-child",
+  "version": "0.0.0",
+  "description": "Run a child as if it's the foreground process.  Give it stdio.  Exit when it exits.",
+  "main": "index.js",
+  "directories": {
+    "test": "test"
+  },
+  "dependencies": {},
+  "devDependencies": {
+    "tap": "^1.0.4"
+  },
+  "scripts": {
+    "test": "tap test/*.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "git+https://github.com/isaacs/foreground-child.git"
+  },
+  "author": "Isaac Z. Schlueter <i at izs.me> (http://blog.izs.me/)",
+  "license": "ISC",
+  "bugs": {
+    "url": "https://github.com/isaacs/foreground-child/issues"
+  },
+  "homepage": "https://github.com/isaacs/foreground-child#readme"
+}
diff --git a/test/basic.js b/test/basic.js
new file mode 100644
index 0000000..0d18edb
--- /dev/null
+++ b/test/basic.js
@@ -0,0 +1,72 @@
+var fg = require('../index.js')
+var spawn = require('child_process').spawn
+
+if (process.argv[2] === 'child') {
+  console.log('stdout')
+  switch (process.argv[3]) {
+  case 'SIGTERM':
+  case 'SIGHUP':
+  case 'SIGKILL':
+    process.kill(process.pid, process.argv[3])
+    break
+  case '0':
+  case '1':
+  case '2':
+    process.exit(+process.argv[3])
+    break
+  }
+
+  return
+}
+
+if (process.argv[2] === 'parent') {
+  var program = process.execPath
+  var args = [__filename, 'child'].concat(process.argv.slice(3))
+  fg(program, args)
+  return
+}
+
+var t = require('tap')
+t.test('signals', function (t) {
+  var signals = [
+    'SIGTERM',
+    'SIGHUP',
+    'SIGKILL'
+  ]
+  signals.forEach(function (sig) {
+    t.test(sig, function (t) {
+      t.plan(3)
+      var prog = process.execPath
+      var args = [__filename, 'parent', sig]
+      var child = spawn(prog, args)
+      var out = ''
+      child.stdout.on('data', function (c) { out += c })
+      child.on('close', function (code, signal) {
+        t.equal(signal, sig)
+        t.equal(code, null)
+        t.equal(out, 'stdout\n')
+      })
+    })
+  })
+  t.end()
+})
+
+t.test('exit codes', 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]
+      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\n')
+      })
+    })
+  })
+  t.end()
+})

-- 
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