forked from dskrepps/requireFrom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (28 loc) · 833 Bytes
/
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
/*
Require from a directory relative to node_modules, flattening your require paths.
Example:
Given project structure:
node_modules/requirefrom/
lib/some/complex/dir/module/module.js
lib/otherModule/index.js
The file module.js:
var lib = require('requirefrom')('lib');
var otherModule = lib('otherModule');
Would be equivalent to:
var otherModule = require('../../../../otherModule');
*/
var path = require('path');
var pkg = require(path.normalize(__dirname + '/../../package.json'));
var requirefrom = function( fromPath ){
return function( modulePath ){
return require( path.normalize(
__dirname + '/../../' + fromPath + '/' + modulePath
) );
}
}
if(pkg.requirefrom) {
for(var dir in pkg.requirefrom) {
requirefrom[dir] = requirefrom(pkg.requirefrom[dir]);
}
}
module.exports = requirefrom;