Skip to content

Commit

Permalink
mafintosh#22 passing execPath and execArgs to child_process.fork
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrombley committed Dec 5, 2017
1 parent 7b899e7 commit 26ca57d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var monitor = respawn(['node', 'server.js'], {
kill:30000, // wait 30s before force killing after stopping
stdio: [...], // forward stdio options
fork: true // fork instead of spawn
execPath: 'exec/path' // executable used to create the child process, if `fork` is `true`
execArgv: [...], // list of string arguments passed to the executable, if `fork` is `true`
})

monitor.start() // spawn and watch
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var Monitor = function(command, opts) {
this.stderr = opts.stderr
this.silent = opts.silent
this.windowsVerbatimArguments = opts.windowsVerbatimArguments
this.execPath = opts.execPath
this.execArgv = opts.execArgv
this.spawnFn = opts.fork
? fork
: spawn
Expand Down Expand Up @@ -118,6 +120,8 @@ Monitor.prototype.start = function() {
gid: self.gid,
stdio: self.stdio,
silent: self.silent,
execPath: self.execPath,
execArgv: self.execArgv,
windowsVerbatimArguments: self.windowsVerbatimArguments
})

Expand Down
4 changes: 4 additions & 0 deletions test/apps/forkExecPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
process.send({
execPath: process.execPath,
execArgv: process.execArgv
})
45 changes: 45 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
var test = require('tap').test
var path = require('path')
var util = require('util')
var os = require('os')
var fs = require('fs')
var respawn = require('../index')

var crash = path.join(__dirname, 'apps', 'crash.js')
var run = path.join(__dirname, 'apps', 'run.js')
var hello = path.join(__dirname, 'apps', 'hello-world.js')
var env = path.join(__dirname, 'apps', 'env.js')
var fork = path.join(__dirname, 'apps', 'fork.js')
var forkExecPath = path.join(__dirname, 'apps', 'forkExecPath.js')
var node = process.execPath

test('restart', function(t) {
Expand Down Expand Up @@ -311,3 +314,45 @@ test('fork', function(t) {

mon.start()
})

test('fork with a custom execPath and execArgv', function(t) {
t.plan(1)

var mon
var messages = []

var symlinkName = os.platform() === 'win32' ? 'node-symlink.exe' : 'node-symlink'
var symlinkPath = path.join(__dirname, symlinkName)
var execArgv = ['--preserve-symlinks']

fs.symlink(node, symlinkName, () => {
mon = respawn([forkExecPath], {
fork:true,
maxRestarts:1,
sleep:1,
execPath: symlinkPath,
execArgv: execArgv
})

mon.on('message', function(message) {
messages.push(message)
})

mon.on('stop', function() {
t.deepEqual(messages, [
{execPath: symlinkPath, execArgv: execArgv},
{execPath: symlinkPath, execArgv: execArgv}
])
})

mon.start()
})

t.tearDown(() => {
if (fs.existsSync(symlinkPath)) {
fs.unlinkSync(symlinkPath, (err) => {
if (err) console.error(err)
})
}
})
})

0 comments on commit 26ca57d

Please sign in to comment.