| 1 |
'use strict'; |
| 2 |
|
| 3 |
const isWindows = require("is-windows") |
| 4 |
const path = require("path") |
| 5 |
const homedir = require("../homedir") |
| 6 |
|
| 7 |
const pathRe = isWindows() ? /^PATH=/i : /^PATH=/; |
| 8 |
|
| 9 |
/** |
| 10 |
* Updates the environment variables to intercept `node` commands and pass down options. |
| 11 |
* |
| 12 |
* @param workingDir {string} Absolute system-dependent path to the directory containing the shim files. |
| 13 |
* @param options {import("../munge").InternalSpawnOptions} Original internal spawn options. |
| 14 |
* @return {import("../munge").InternalSpawnOptions} Updated internal spawn options. |
| 15 |
*/ |
| 16 |
function mungeEnv(workingDir, options) { |
| 17 |
let pathEnv |
| 18 |
|
| 19 |
const envPairs = options.envPairs.map((ep) => { |
| 20 |
if (pathRe.test(ep)) { |
| 21 |
// `PATH` env var: prefix its value with `workingDir` |
| 22 |
// `5` corresponds to the length of `PATH=` |
| 23 |
pathEnv = ep.substr(5) |
| 24 |
const k = ep.substr(0, 5) |
| 25 |
return k + workingDir + path.delimiter + pathEnv |
| 26 |
} else { |
| 27 |
// Return as-is |
| 28 |
return ep; |
| 29 |
} |
| 30 |
}); |
| 31 |
|
| 32 |
if (pathEnv === undefined) { |
| 33 |
envPairs.push((isWindows() ? 'Path=' : 'PATH=') + workingDir) |
| 34 |
} |
| 35 |
if (options.originalNode) { |
| 36 |
const key = path.basename(workingDir).substr('.node-spawn-wrap-'.length) |
| 37 |
envPairs.push('SW_ORIG_' + key + '=' + options.originalNode) |
| 38 |
} |
| 39 |
|
| 40 |
envPairs.push('SPAWN_WRAP_SHIM_ROOT=' + homedir) |
| 41 |
|
| 42 |
if (process.env.SPAWN_WRAP_DEBUG === '1') { |
| 43 |
envPairs.push('SPAWN_WRAP_DEBUG=1') |
| 44 |
} |
| 45 |
|
| 46 |
return {...options, envPairs}; |
| 47 |
} |
| 48 |
|
| 49 |
module.exports = mungeEnv |