Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect shebangs absolutely to node, wrap those too #13

Merged
merged 4 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var path = require('path')
var signalExit = require('signal-exit')
var homedir = require('os-homedir')() + '/.node-spawn-wrap-'
var winRebase = require('./lib/win-rebase')
var which = require('which')

var cmdname = path.basename(process.execPath, '.exe')

Expand Down Expand Up @@ -72,6 +73,13 @@ function wrap (argv, env, workingDir) {
exe === cmdname) {
c = c.replace(re, '$1 ' + workingDir + '/node')
options.args[cmdi + 1] = c
} else if (exe === 'npm' && !isWindows) {
var npmPath = whichOrUndefined('npm')

if (npmPath) {
c = c.replace(re, '$1 ' + workingDir + '/node ' + npmPath)
options.args[cmdi + 1] = c
}
}
}
}
Expand Down Expand Up @@ -119,6 +127,27 @@ function wrap (argv, env, workingDir) {
options.file = workingDir + '/' + file
options.args[0] = workingDir + '/' + file
}

} else {
try {
var resolved = which.sync(options.file)
} catch (er) {}
if (resolved) {
var shebang = fs.readFileSync(resolved, 'utf8')
var match = shebang.match(/^#!([^\r\n]+)/)
if (match) {
var shebangbin = match[1].split(' ')[0]
var maybeNode = path.basename(shebangbin)
if (maybeNode === 'node' || maybeNode === 'iojs' || cmdname === maybeNode) {

options.file = shebangbin
options.args = [shebangbin, workingDir + '/' + maybeNode]
.concat(resolved)
.concat(match[1].split(' ').slice(1))
.concat(options.args)
}
}
}
}

for (var i = 0; i < options.envPairs.length; i++) {
Expand All @@ -133,6 +162,17 @@ function wrap (argv, env, workingDir) {
options.envPairs.push((isWindows ? 'Path=' : 'PATH=') + workingDir)
}

if (file === 'npm' && !isWindows) {
var npmPath = whichOrUndefined('npm')

if (npmPath) {
options.args[0] = npmPath

options.file = workingDir + '/node'
options.args.unshift(workingDir + '/node')
}
}

if (isWindows) fixWindowsBins(workingDir, options)

return spawn.call(this, options)
Expand All @@ -141,6 +181,14 @@ function wrap (argv, env, workingDir) {
return unwrap
}

function whichOrUndefined (executable) {
var path
try {
path = which.sync(executable)
} catch (er) {}
return path
}

// by default Windows will reference the full
// path to the node.exe or iojs.exe as the bin,
// we should instead point spawn() at our .cmd shim.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"mkdirp": "^0.5.0",
"os-homedir": "^1.0.1",
"rimraf": "^2.3.3",
"signal-exit": "^2.0.0"
"signal-exit": "^2.0.0",
"which": "^1.2.4"
},
"scripts": {
"test": "tap test/*.js"
Expand Down
67 changes: 67 additions & 0 deletions test/abs-shebang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var path = require('path')
var fs = require('fs')
var spawn = require('child_process').spawn
var t = require('tap')
var node = process.execPath
var wrap = require.resolve('./fixtures/wrap.js')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var fs = require('fs')

if (process.platform === 'windows') {
t.plan(0, 'No proper shebang support on windows, so skip this')
process.exit(0)
}

var expect =
'before in shim\n' +
'shebang main\n' +
'after in shim\n' +
'before in shim\n' +
'shebang main\n' +
'after in shim\n'

var fixdir = path.resolve(__dirname, 'fixtures', 'shebangs')

t.test('setup', function (t) {
rimraf.sync(fixdir)
mkdirp.sync(fixdir)
t.end()
})

t.test('absolute', function (t) {
var file = path.resolve(fixdir, 'absolute.js')
runTest(file, process.execPath, t)
})

t.test('env', function (t) {
var file = path.resolve(fixdir, 'env.js')
runTest(file, '/usr/bin/env node', t)
})

function runTest (file, shebang, t) {
var content = '#!' + shebang + '\nconsole.log("shebang main")\n'
fs.writeFileSync(file, content, 'utf8')
fs.chmodSync(file, '0755')
var child = spawn(node, [wrap, file])
var out = ''
var err = ''
child.stdout.on('data', function (c) {
out += c
})
child.stderr.on('data', function (c) {
err += c
})
child.on('close', function (code, signal) {
t.equal(code, 0)
t.equal(signal, null)
t.equal(out, expect)
// console.error(err)
t.end()
})
}

t.test('cleanup', function (t) {
rimraf.sync(fixdir)
t.end()
})
36 changes: 36 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var winNoSig = isWindows && 'no signals get through cmd'
var onExit = require('signal-exit')
var cp = require('child_process')
var fixture = require.resolve('./fixtures/script.js')
var npmFixture = require.resolve('./fixtures/npm')
var fs = require('fs')
var path = require('path')

Expand Down Expand Up @@ -293,6 +294,41 @@ t.test('exec shebang', { skip: winNoShebang }, function (t) {
})
})

// see: https://github.com/bcoe/nyc/issues/190
t.test('Node 5.8.x + npm 3.7.x - spawn', { skip: winNoShebang }, function (t) {
var npmdir = path.dirname(npmFixture)
process.env.PATH = npmdir + ':' + (process.env.PATH || '')
var child = cp.spawn('npm', ['xyz'])

var out = ''
child.stdout.on('data', function (c) {
out += c
})
child.on('close', function (code, signal) {
t.equal(code, 0)
t.equal(signal, null)
t.true(~out.indexOf('xyz'))
t.end()
})
})

t.test('Node 5.8.x + npm 3.7.x - shell', { skip: winNoShebang }, function (t) {
var npmdir = path.dirname(npmFixture)
process.env.PATH = npmdir + ':' + (process.env.PATH || '')
var child = cp.exec('npm xyz')

var out = ''
child.stdout.on('data', function (c) {
out += c
})
child.on('close', function (code, signal) {
t.equal(code, 0)
t.equal(signal, null)
t.true(~out.indexOf('xyz'))
t.end()
})
})

t.test('--harmony', function (t) {
var node = process.execPath
var child = cp.spawn(node, ['--harmony', fixture, 'xyz'])
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/npm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
// 2>/dev/null; exec "`dirname "$0"`/node" "$0" "$@"
console.log('%j', process.execArgv)
console.log('%j', process.argv.slice(2))
setTimeout(function () {}, 100)
3 changes: 3 additions & 0 deletions test/fixtures/test-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log('before in shim')
require('../..').runMain()
console.log('after in shim')
27 changes: 27 additions & 0 deletions test/fixtures/wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node
var sw = require('../..')

sw([require.resolve('./test-shim.js')])

var path = require('path')
var spawn = require('child_process').spawn

spawn(path.resolve(process.argv[2]), process.argv.slice(3), {
stdio: 'inherit'
}).on('close', function (code, signal) {
if (code || signal) {
throw new Error('failed with ' + (code || signal))
}

// now run using PATH
process.env.PATH = path.resolve(path.dirname(process.argv[2])) +
':' + process.env.PATH

spawn(path.basename(process.argv[2]), process.argv.slice(3), {
stdio: 'inherit',
}, function (code, signal) {
if (code || signal) {
throw new Error('failed with ' + (code || signal))
}
})
})