1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
Description: module foreground-child
see copyright for more information
Last-Update: 2016-11-11
Forwarded: not-needed
Author: Jérémy Lal <kapouer@melix.org>
--- /dev/null
+++ b/node_modules/foreground-child.js
@@ -0,0 +1,80 @@
+var signalExit = require('signal-exit')
+var spawn = require('child_process').spawn
+if (process.platform === 'win32') {
+ spawn = require('cross-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) {
+ return done()
+ }
+
+ if (Array.isArray(program)) {
+ args = program.slice(1)
+ program = program[0]
+ } else if (!Array.isArray(args)) {
+ args = [].slice.call(arguments, 1, arrayIndex)
+ }
+
+ var spawnOpts = { stdio: [0, 1, 2] }
+
+ if (process.send) {
+ spawnOpts.stdio.push('ipc')
+ }
+
+ var child = spawn(program, args, spawnOpts)
+
+ var childExited = false
+ signalExit(function (code, signal) {
+ child.kill(signal || 'SIGHUP')
+ })
+
+ child.on('close', function (code, signal) {
+ // Allow the callback to inspect the child’s exit code and/or modify it.
+ process.exitCode = signal ? 128 + signal : 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 {
+ // Equivalent to process.exit() on Node.js >= 0.11.8
+ process.exit(process.exitCode)
+ }
+ })
+ })
+
+ if (process.send) {
+ process.removeAllListeners('message')
+
+ child.on('message', function (message, sendHandle) {
+ process.send(message, sendHandle)
+ })
+
+ process.on('message', function (message, sendHandle) {
+ child.send(message, sendHandle)
+ })
+ }
+
+ return child
+}
|