-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-load.js
63 lines (50 loc) · 1.73 KB
/
build-load.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
55
56
57
58
59
60
61
62
63
var fs = require('fs');
var os = require('os');
var path = require('path');
const runtime = getRuntime();
const abi = process.versions.modules;
const platform = os.platform();
const arch = os.arch();
function load (dir, prebuildMatcher, prebuildDirBuilder) {
return require(getPath(dir, prebuildMatcher, prebuildDirBuilder));
}
function getPath(dir, prebuildMatcher, prebuildDirBuilder) {
dir = path.resolve(dir || '.');
var release = findFirst(path.join(dir, 'build/Release'), matchBuild);
if (release)
return release;
var debug = findFirst(path.join(dir, 'build/Debug'), matchBuild);
if (debug)
return debug;
var prebuild = findFirst(path.join(dir, (prebuildDirBuilder || buildDir)(runtime, abi, platform, arch)), prebuildMatcher || matchNapiPrebuild);
if (prebuild)
return prebuild;
throw new Error(`No native build was found for runtime ${runtime} | ABI ${abi} | ${platform} platform | arch ${arch}`);
}
function buildDir(runtime, abi, platform, arch) {
return `prebuilds/${platform}-${arch}`;
}
function matchNapiPrebuild(name) {
let str = name.split('.')
return str[0] === runtime && str[1] === 'napi' && str[2] == 'node';
}
function findFirst(dir, filter) {
try {
let files = fs.readdirSync(dir).filter(filter);
return files[0] && path.join(dir, files[0])
} catch (err) {
return null;
}
}
function matchBuild(name) {
return name.endsWith('.node');
}
function getRuntime() {
if(process.versions && process.versions.electron)
return 'electron';
return (typeof window !== 'undefined'
&& window.process
&& window.process.type === 'renderer') ?
'electron' : 'node';
}
module.exports = load;