aboutsummaryrefslogtreecommitdiff
path: root/node_modules/spawn-wrap/shim.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/spawn-wrap/shim.js')
-rw-r--r--node_modules/spawn-wrap/shim.js51
1 files changed, 46 insertions, 5 deletions
diff --git a/node_modules/spawn-wrap/shim.js b/node_modules/spawn-wrap/shim.js
index 96437215c..dda6b2ae2 100644
--- a/node_modules/spawn-wrap/shim.js
+++ b/node_modules/spawn-wrap/shim.js
@@ -1,3 +1,5 @@
+'use strict'
+
// This module should *only* be loaded as a main script
// by child processes wrapped by spawn-wrap. It sets up
// argv to include the injected argv (including the user's
@@ -9,24 +11,47 @@
// a require('spawn-wrap').runMain() function that will strip
// off the injected arguments and run the main file.
+// wrap in iife for babylon to handle module-level return
+;(function () {
+
if (module !== require.main) {
throw new Error('spawn-wrap: cli wrapper invoked as non-main script')
}
-// require('fs').createWriteStream('/dev/tty').write('WRAP ' + process.argv.slice(2).join(' ') + '\n')
+var util
+var doDebug = process.env.SPAWN_WRAP_DEBUG === '1'
+var fs
+function debug () {
+ if (!doDebug)
+ return
+
+ if (!fs) {
+ fs = require('fs')
+ util = require('util')
+ }
+
+ var message = util.format.apply(util, arguments).trim()
+ var pref = 'SW ' + process.pid + ': '
+ message = pref + message.split('\n').join('\n' + pref)
+ fs.writeSync(2, message + '\n')
+}
+
+debug('shim', [process.argv[0]].concat(process.execArgv, process.argv.slice(1)))
+
var Module = require('module')
var assert = require('assert')
var path = require('path')
-var node = process.execPath
var settings = require('./settings.json')
var foregroundChild = require(settings.deps.foregroundChild)
var argv = settings.argv
var nargs = argv.length
var env = settings.env
+var key = settings.key
+var node = process.env['SW_ORIG_' + key] || process.execPath
-for (var key in env) {
- process.env[key] = env[key]
+for (var k in env) {
+ process.env[k] = env[k]
}
var needExecArgv = settings.execArgv || []
@@ -34,9 +59,13 @@ var needExecArgv = settings.execArgv || []
// If the user added their OWN wrapper pre-load script, then
// this will pop that off of the argv, and load the "real" main
function runMain () {
+ debug('runMain pre', process.argv)
process.argv.splice(1, nargs)
process.argv[1] = path.resolve(process.argv[1])
+ delete require.cache[process.argv[1]]
+ debug('runMain post', process.argv)
Module.runMain()
+ debug('runMain after')
}
// Argv coming in looks like:
@@ -54,6 +83,7 @@ for (var a = 2; !hasMain && a < process.argv.length; a++) {
case '--interactive':
case '--eval':
case '-e':
+ case '-p':
case '-pe':
hasMain = false
a = process.argv.length
@@ -74,6 +104,7 @@ for (var a = 2; !hasMain && a < process.argv.length; a++) {
}
}
}
+debug('after argv parse hasMain=%j', hasMain)
if (hasMain > 2) {
// if the main file is above #2, then it means that there
@@ -88,6 +119,7 @@ if (hasMain > 2) {
if (!hasMain) {
// we got loaded by mistake for a `node -pe script` or something.
var args = process.execArgv.concat(needExecArgv, process.argv.slice(2))
+ debug('no main file!', args)
foregroundChild(node, args)
return
}
@@ -99,6 +131,7 @@ if (!hasMain) {
if (needExecArgv.length) {
var pexec = process.execArgv
if (JSON.stringify(pexec) !== JSON.stringify(needExecArgv)) {
+ debug('need execArgv for this', pexec, '=>', needExecArgv)
var spawn = require('child_process').spawn
var sargs = pexec.concat(needExecArgv).concat(process.argv.slice(1))
foregroundChild(node, sargs)
@@ -132,8 +165,16 @@ if (isWindows) {
var spawnWrap = require(settings.module)
if (nargs) {
- spawnWrap.runMain = runMain
+ spawnWrap.runMain = function (original) { return function () {
+ spawnWrap.runMain = original
+ runMain()
+ }}(spawnWrap.runMain)
}
spawnWrap(argv, env, __dirname)
+debug('shim runMain', process.argv)
+delete require.cache[process.argv[1]]
Module.runMain()
+
+// end iife wrapper for babylon
+})()