This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (41 loc) · 1.42 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
var fs = require('fs');
var path = require('path');
var Logger = require('debug-logger')('nqbs');
function requireNativeModule(name, forceConf) {
// Search relative to the file that included this one
const base = path.dirname(module.parent.filename);
// Suffixes to search for (in each mode)
// Both are used, debug just changes which is tried first
name = name.replace(/\.node$/, '');
let search = {
release: path.join('addon-build', 'release', 'install-root', name + '.node'),
debug: path.join('addon-build', 'debug', 'install-root', name + '.node'),
default: path.join('addon-build', 'default', 'install-root', name + '.node'),
};
if (typeof forceConf !== 'undefined') {
search = { };
search[forceConf] = path.join('addon-build', forceConf, 'install-root', name + '.node');
}
let root = base;
let location;
let same = 0;
let found = false;
// Walk upward to the root
while (same < 2 || found) {
Logger.log('Search module for configuration:', conf);
for (var conf in search) {
try {
location = path.join(root, search[conf])
Logger.log(' at', location)
found = fs.statSync(location)
} catch (e) { }
if (found) break
}
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;