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

46 lines
1.1 KiB
JavaScript
Raw Normal View History

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