forked from cjntaylor/node-cmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (42 loc) · 1.23 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
'use strict';
var fs = require('fs');
var path = require('path');
function requireNativeModule(name, debug, searchPath) {
if (searchPath === undefined)
{
searchPath = path.dirname(module.parent.filename)
}
// Search relative to the file that included this one
var base = path.resolve(searchPath);
// Suffixes to search for (in each mode)
// Both are used, debug just changes which is tried first
var search = {
debug: path.join('build', 'Debug', name + '.node'),
release: path.join('build', 'Release', name + '.node')
};
var root = base;
var location;
var same = 0;
var found = false;
// Walk upward to the root of the current drive
while(same < 2 || found) {
try {
location = path.join(root, (debug) ? search.debug : search.release);
found = fs.statSync(location);
}
catch(e) {}
if(!found) {
try {
location = path.join(root, (debug) ? search.release : search.debug);
found = fs.statSync(location);
}
catch(e) {}
}
if(found) break;
root = path.dirname(root);
if(root == path.dirname(root)) same++;
}
if(!found) throw new Error('Unable to find native module');
return require(location);
}
module.exports = requireNativeModule;