forked from prebuild/node-gyp-build
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
72 lines (55 loc) · 2.01 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var fs = require('fs')
var path = require('path')
var os = require('os')
var abi = process.versions.modules // TODO: support old node where this is undef
var runtime = isElectron() ? 'electron' : 'node'
var arch = os.arch()
var platform = os.platform()
module.exports = load
function load (dir) {
return require(load.path(dir))
}
load.path = function (dir) {
dir = path.resolve(dir || '.')
try {
var name = require(path.join(dir, 'package.json')).name.toUpperCase().replace(/-/g, '_')
if (process.env[name + '_PREBUILD']) dir = process.env[name + '_PREBUILD']
} catch (err) {}
var release = getFirst(path.join(dir, 'build/Release'), matchBuild)
if (release) return release
var debug = getFirst(path.join(dir, 'build/Debug'), matchBuild)
if (debug) return debug
var prebuild = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchPrebuild)
if (prebuild) return prebuild
var napiRuntime = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchNapiRuntime)
if (napiRuntime) return napiRuntime
var napi = getFirst(path.join(dir, 'prebuilds/' + platform + '-' + arch), matchNapi)
if (napi) return napi
throw new Error('No native build was found for runtime=' + runtime + ' abi=' + abi + ' platform=' + platform + ' arch=' + arch)
}
function getFirst (dir, filter) {
try {
var files = fs.readdirSync(dir).filter(filter)
return files[0] && path.join(dir, files[0])
} catch (err) {
return null
}
}
function matchNapiRuntime (name) {
return name === runtime + '-napi.node'
}
function matchNapi (name) {
return name === 'node-napi.node'
}
function matchPrebuild (name) {
var parts = name.split('-')
return parts[0] === runtime && parts[1] === abi + '.node'
}
function matchBuild (name) {
return /\.node$/.test(name)
}
function isElectron () {
if (process.versions && process.versions.electron) return true
if (process.env.ELECTRON_RUN_AS_NODE) return true
return typeof window !== 'undefined' && window.process && window.process.type === 'renderer'
}