Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support jumping between node.js versions (#7)
Use `NODE_PATH` and use bare filename for `--require` if it is needed in node.js < 12 or if node.js 12+ would otherwise require quoting the require path. This should avoid a crash when `node-preload` is setup in node.js 12 but then a test is run under node.js < 12. It should also ensure compatibility when setup is performed in node.js < 12 and tests are later run under node.js 12+. Ref istanbuljs/nyc#1246
- Loading branch information
1 parent
62de5ed
commit 68950a0
Showing
6 changed files
with
41 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,31 @@ | ||
'use strict'; | ||
|
||
/* istanbul ignore next: version specific branching */ | ||
const requireType = Number(process.versions.node.split('.')[0]) < 12 ? 'legacy' : 'modern'; | ||
const path = require('path'); | ||
|
||
module.exports = require(`./generate-require-${requireType}.js`); | ||
const needsPathRegExp = /[\\ "]/; | ||
|
||
const needsPathEnv = dir => needsPathRegExp.test(dir); | ||
|
||
function generateRequire(filename) { | ||
if (needsPathEnv(filename)) { | ||
return `--require ${path.basename(filename)}`; | ||
} | ||
|
||
return `--require ${filename}`; | ||
} | ||
|
||
function processNodePath(value) { | ||
const dir = path.dirname(require.resolve('./preload-path/node-preload.js')); | ||
const existing = value === '' ? [] : value.split(path.delimiter); | ||
if (existing.includes(dir)) { | ||
return value; | ||
} | ||
|
||
return existing.concat(dir).join(path.delimiter); | ||
} | ||
|
||
module.exports = { | ||
generateRequire, | ||
processNodePath, | ||
needsPathEnv | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters