electron/lib/common/reset-search-paths.js

37 lines
1 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
const path = require('path')
const Module = require('module')
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Clear Node's global search paths.
2016-03-25 19:57:17 +00:00
Module.globalPaths.length = 0
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Clear current and parent(init.coffee)'s search paths.
2016-03-25 19:57:17 +00:00
module.paths = []
2016-01-12 02:40:23 +00:00
2016-03-25 19:57:17 +00:00
module.parent.paths = []
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Prevent Node from adding paths outside this app to search paths.
2016-03-25 19:57:17 +00:00
Module._nodeModulePaths = function (from) {
var dir, i, part, parts, paths, skipOutsidePaths, splitRe, tip
from = path.resolve(from)
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// If "from" is outside the app then we do nothing.
2016-03-25 19:57:17 +00:00
skipOutsidePaths = from.startsWith(process.resourcesPath)
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// Following logoic is copied from module.js.
2016-03-25 19:57:17 +00:00
splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//
paths = []
parts = from.split(splitRe)
2016-01-12 02:40:23 +00:00
for (tip = i = parts.length - 1; i >= 0; tip = i += -1) {
2016-03-25 19:57:17 +00:00
part = parts[tip]
2016-01-12 02:40:23 +00:00
if (part === 'node_modules') {
2016-03-25 19:57:17 +00:00
continue
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
dir = parts.slice(0, tip + 1).join(path.sep)
2016-01-12 02:40:23 +00:00
if (skipOutsidePaths && !dir.startsWith(process.resourcesPath)) {
2016-03-25 19:57:17 +00:00
break
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
paths.push(path.join(dir, 'node_modules'))
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
return paths
}