Filter existing search paths instead reimplementing

This commit is contained in:
Kevin Sawicki 2016-06-23 15:27:45 -07:00
parent eed240be1c
commit b273b70eee
2 changed files with 10 additions and 22 deletions

View file

@ -9,30 +9,19 @@ module.paths = []
module.parent.paths = []
// Prevent Node from adding paths outside this app to search paths.
const originalNodeModulePaths = Module._nodeModulePaths
Module._nodeModulePaths = function (from) {
from = path.resolve(from)
const paths = originalNodeModulePaths(from)
const rootPath = process.resourcesPath
// If "from" is outside the app then we do nothing.
const skipOutsidePaths = from.startsWith(process.resourcesPath)
// Following logic is copied from module.js.
const splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//
const paths = []
const parts = from.split(splitRe)
let tip
let i
for (tip = i = parts.length - 1; i >= 0; tip = i += -1) {
const part = parts[tip]
if (part === 'node_modules') {
continue
}
const dir = parts.slice(0, tip + 1).join(path.sep)
if (skipOutsidePaths && !dir.startsWith(process.resourcesPath)) {
break
}
paths.push(path.join(dir, 'node_modules'))
const skipOutsidePaths = path.resolve(from).startsWith(rootPath)
if (skipOutsidePaths) {
return paths.filter(function (candidate) {
return candidate.startsWith(rootPath)
})
}
return paths
}