Skip to content

Commit

Permalink
chore: Update node-preload and use process-on-spawn (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored Dec 17, 2019
1 parent 5258e9f commit d44ff19
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 326 deletions.
19 changes: 12 additions & 7 deletions bin/nyc.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,20 @@ async function main () {
}

if (!argv.useSpawnWrap) {
const { preloadAppend, propagateEnv } = require('node-preload')

nyc.require.forEach(requireModule => {
const mod = resolveFrom.silent(nyc.cwd, requireModule) || requireModule
preloadAppend(mod)
const requireModules = [
require.resolve('../lib/register-env.js'),
...nyc.require.map(mod => resolveFrom.silent(nyc.cwd, mod) || mod)
]
const preloadList = require('node-preload')
preloadList.push(
...requireModules,
require.resolve('../lib/wrap.js')
)

Object.assign(process.env, env)
requireModules.forEach(mod => {
require(mod)
})
preloadAppend(require.resolve('../lib/wrap.js'))
Object.assign(propagateEnv, env)
}

if (argv.all) {
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ class NYC {
// This is a bug with the spawn-wrap method where
// we cannot force propagation of NYC_PROCESS_ID.
if (!this.config.useSpawnWrap) {
const { propagateEnv } = require('node-preload')
propagateEnv.NYC_PROCESS_ID = this.processInfo.uuid
const updateVariable = require('./lib/register-env.js')
updateVariable('NYC_PROCESS_ID')
}
this._addRequireHooks()
this._wrapExit()
Expand Down
27 changes: 27 additions & 0 deletions lib/register-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const processOnSpawn = require('process-on-spawn')

const envToCopy = {}

processOnSpawn.addListener(({ env }) => {
Object.assign(env, envToCopy)
})

const copyAtLoad = [
'NYC_CONFIG',
'NYC_CWD',
'NYC_PROCESS_ID',
'BABEL_DISABLE_CACHE',
'NYC_PROCESS_ID'
]

for (const env of copyAtLoad) {
if (env in process.env) {
envToCopy[env] = process.env[env]
}
}

module.exports = function updateVariable (envName) {
envToCopy[envName] = process.env[envName]
}
Loading

0 comments on commit d44ff19

Please sign in to comment.