-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
52 lines (38 loc) · 1.16 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
46
47
48
49
50
51
52
/*
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 requireFrom = module.exports = function requireFrom( fromPath ){
return function requireModule( modulePath ){
return require( path.normalize(
__dirname + '/../../' + fromPath + '/' + modulePath
) );
}
}
// Extra feature. Add requireFrom: {"lib": "some/lib/dir/"}
// in package.json and then:
// var rf = require('requirefrom').readPkg();
// var module = rf.lib('myModule.js');
requireFrom.readPkg = function( prop ){
var pkg, dirs, dir;
prop = prop || 'requireFrom';
try{ pkg = require(__dirname + '/../../package.json'); }
catch(e){ throw new Error('requireFrom couldn\'t find package.json'); }
dirs = pkg[prop];
if(dirs){
for(dir in dirs){
requireFrom[dir] = requireFrom(dirs[dir]);
}
}
return requireFrom;
};