-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
45 lines (36 loc) · 1.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var Module = require('module').Module;
var nodePath = require('path');
var appModulePaths = [];
var old_nodeModulePaths = Module._nodeModulePaths;
Module._nodeModulePaths = function(from) {
var paths = old_nodeModulePaths.call(this, from);
// Only include the app module path for top-level modules
// that were not installed:
if (from.indexOf('node_modules') === -1) {
paths = appModulePaths.concat(paths);
}
return paths;
};
function addPath(path) {
function addPathHelper(targetArray) {
path = nodePath.normalize(path);
if (targetArray && targetArray.indexOf(path) === -1) {
targetArray.unshift(path);
}
}
var parent;
path = nodePath.normalize(path);
if (appModulePaths.indexOf(path) === -1) {
appModulePaths.push(path);
// Enable the search path for the current top-level module
addPathHelper(require.main.paths);
parent = module.parent;
// Also modify the paths of the module that was used to load the app-module-paths module
// and all of it's parents
while(parent && parent !== require.main) {
addPathHelper(parent.paths);
parent = parent.parent;
}
}
}
exports.addPath = addPath;