-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind.js
More file actions
35 lines (28 loc) · 798 Bytes
/
find.js
File metadata and controls
35 lines (28 loc) · 798 Bytes
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
var path = require('path');
var relative = require('require-relative');
var project = process.argv[2] || ''; // project root path
var file = process.argv[3] || ''; // project file name eg: '/Users/project/index.js'
var ref = process.argv[4] || ''; // ref name eg: 'react'
// project absolute path
// eg: require('/module/file')
if (ref.match(/^\//)) {
ref = path.join(project, ref);
}
var ret;
try {
ret = relative.resolve(ref, file);
} catch(ex) {
}
// sometimes module's ref is sibling. (npm flat install)
// eg:
// node_modules
// a/index.js
// b
// in a/index.js require('b')
if (!ret && ref.match(/^[^\.\/]/) && file.match(/node_modules/)) {
try {
ret = relative.resolve(ref, file.replace(/\/node_modules\/.*?$/, ''));
} catch(ex) {
}
}
console.log(ret || '');