machinepack-process
Advanced tools
Comparing version 3.0.0-3 to 3.0.0-4
@@ -109,3 +109,3 @@ module.exports = { | ||
// Remove the `close` listener from the child process, so that it's not called later | ||
// if that signal comes in belatedly. | ||
// if that signal comes in belatedly. (And so we don't leave dangling listeners hanging around.) | ||
inputs.childProcess.removeListener('close', handleClosingChildProc); | ||
@@ -141,2 +141,6 @@ | ||
// Remove the `close` listener from the child process, so that we don't leave any | ||
// event listeners dangling. | ||
inputs.childProcess.removeListener('close', handleClosingChildProc); | ||
// Clear the timeout timer. | ||
@@ -166,3 +170,3 @@ clearTimeout(timer); | ||
// Listen for the child process being closed. | ||
inputs.childProcess.on('close', handleClosingChildProc); | ||
inputs.childProcess.once('close', handleClosingChildProc); | ||
@@ -169,0 +173,0 @@ // Set a timer. |
@@ -28,3 +28,3 @@ module.exports = { | ||
description: 'The command to run in the child process, without any CLI arguments or options.', | ||
extendedDescription: 'Node core is tolerant of CLI args mixed in with the main "command" in `child_process.exec()`, but it is not so forgiving when using `child_process.spawn()`.', | ||
extendedDescription: 'Node core is tolerant of CLI args mixed in with the main "command" in `child_process.exec()`, but it is not so forgiving when using `child_process.spawn()`. That means you cannot provide a command like "git commit" this way. Instead, provide "git" as the command and `["commit"]` as the CLI args.', | ||
example: 'ls', | ||
@@ -82,3 +82,10 @@ required: true | ||
// First, build up the options to pass in to `child_process.spawn()`. | ||
// First, validate that the provided command is valid. | ||
// `child_process.spawn()` has some pretty harsh limitations here, | ||
// and since this interface is so low level, it's really easy to mess up. | ||
if (inputs.command.match(/\s/)) { | ||
return exits.error(new Error('The string provided to spawnChildProcess() for the `command` input ("'+inputs.command+'") is not valid. Unlike `executeCommand()` (the simpler, higher-level machine), the `command` provided to `spawnChildProcess()` should never contain spaces! To spawn a child process with a command like `git commit`, send in "git" as the command, and `["commit", "-am", "foo"]` as the CLI args.')); | ||
} | ||
// Now build up the options to pass in to `child_process.spawn()`. | ||
var childProcOpts = {}; | ||
@@ -106,6 +113,15 @@ | ||
// Then spawn the child process and set up a no-op error listener to prevent crashing. | ||
// Then spawn the child process. | ||
var liveChildProc = spawn(inputs.command, inputs.cliArgs, childProcOpts); | ||
liveChildProc.on('error', function wheneverAnErrorIsEmitted(err){ /* ... */ }); | ||
// Set up a no-op error listener to prevent crashing. | ||
var wheneverAnErrorIsEmitted = function (err){ /* ... */ }; | ||
liveChildProc.on('error', wheneverAnErrorIsEmitted); | ||
// But because we're binding that no-op error listener, we'll also need to bind | ||
// a one-time-use `close` listener; purely to _unbind_ our `error` listener. | ||
liveChildProc.once('close', function (){ | ||
liveChildProc.removeListener('error', wheneverAnErrorIsEmitted); | ||
}); | ||
// Return live child process through the `success` exit. | ||
@@ -112,0 +128,0 @@ return exits.success(liveChildProc); |
{ | ||
"name": "machinepack-process", | ||
"version": "3.0.0-3", | ||
"version": "3.0.0-4", | ||
"description": "Work with child procs and the running process.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -145,2 +145,32 @@ /** | ||
describe('if spaces are used in the command', function (){ | ||
this.slow(1500); | ||
var childProc; | ||
it('should fail with an error', function (done){ | ||
try { | ||
childProc = Pack.spawnChildProcess({ | ||
command: 'blah blah, space separated args arent allowed see', | ||
}).execSync(); | ||
} | ||
catch (e) { | ||
return done(); | ||
} | ||
return done(new Error('Should have failed w/ an error!')); | ||
}); | ||
after(function (done) { | ||
if (!childProc) { return done(); } | ||
Pack.killChildProcess({ | ||
childProcess: childProc, | ||
forceIfNecessary: true | ||
}).exec(done); | ||
}); | ||
});//</if spaces are used in the command> | ||
describe('if the child process emits an error', function (){ | ||
@@ -152,3 +182,4 @@ this.slow(1500); | ||
childProc = Pack.spawnChildProcess({ | ||
command: 'blah blah, space separated args arent allowed see, so this will be emitting errors for sure!!!!', | ||
command: 'node', | ||
cliArgs: ['-e', 'throw new Error(\'oops\');'], | ||
}).execSync(); | ||
@@ -155,0 +186,0 @@ // We wait another few moments just to be 100% sure. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37988
703