-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinding.js
54 lines (43 loc) · 1.36 KB
/
binding.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
53
54
var fs = require('fs'),
path = require('path');
var name = 'nil';
var paths = [
{ build: true, path: ['build', 'Release'] },
{ build: false, path: ['prebuilt', process.version.slice(0, 4), process.platform, process.arch] },
{ build: true, path: [] }
];
if (process.env.npm_lifecycle_event === 'install' || process.env.npm_lifecycle_event === 'update') {
runner(builder);
} else {
runner(exporter);
}
function exporter(pathinfo){
if (pathinfo.exists) {
module.exports = require(pathinfo.path);
return true;
}
}
function builder(pathinfo){
if (!pathinfo.build) return;
var PATH = process.env.PATH.split(process.platform === 'win32' ? ';' : ':'),
script = process.platform === 'win32' ? 'node-gyp.cmd' : 'node-gyp',
gyp;
while (PATH.length) {
if (fs.existsSync(gyp = path.join(PATH.pop(), script))) {
require('child_process').spawn(gyp, ['rebuild'], {
cwd: __dirname,
env: process.env,
stdio: 'inherit'
});
return true;
}
}
}
function runner(handler){
for (var i=0; i < paths.length; i++) {
paths[i].path = path.resolve(__dirname, paths[i].path.join('/'), name+'.node');
paths[i].exists = fs.existsSync(paths[i].path);
if (handler(paths[i])) return;
}
throw new Error('Bindings not found. Tried:\n [ '+paths.map(function(o){ return o.path }).join(',\n ')+' ]');
}