-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
76 lines (55 loc) · 1.82 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict'
// TODO: Extract this file to an external library.
const { existsSync, readdirSync } = require('fs')
const os = require('os')
const path = require('path')
const PLATFORM = os.platform()
const ARCH = process.arch
const LIBC = PLATFORM === 'linux' ? existsSync('/etc/alpine-release') ? 'musl' : 'glibc' : ''
const ABI = process.versions.modules
const inWebpack = typeof __webpack_require__ === 'function'
const runtimeRequire = inWebpack ? __non_webpack_require__ : require
function maybeLoad (name) {
try {
return load(name)
} catch (e) {
// Not found, skip.
}
}
function load (name) {
const filename = find(name)
if (!filename) {
throw new Error(`Could not find a ${name} binary for ${PLATFORM}${LIBC}-${ARCH}.`)
}
return runtimeRequire(filename)
}
function find (name, binary = false) {
const root = __dirname
const filename = binary ? name : `${name}.node`
const build = `${root}/build/Release/${filename}`
if (existsSync(build)) return build
const folder = findFolder(root)
if (!folder) return
const prebuildFolder = path.join(root, 'prebuilds', folder)
const file = findFile(prebuildFolder, name, binary)
if (!file) return
return path.join(prebuildFolder, file)
}
function findFolder (root) {
try {
const prebuilds = path.join(root, 'prebuilds')
const folders = readdirSync(prebuilds)
return folders.find(f => f === `${PLATFORM}${LIBC}-${ARCH}`)
|| folders.find(f => f === `${PLATFORM}-${ARCH}`)
} catch (e) {
return null
}
}
function findFile (root, name, binary = false) {
const files = readdirSync(root)
if (binary) return files.find(f => f === name)
return files.find(f => f === `${name}-${ABI}.node`)
|| files.find(f => f === `${name}-napi.node`)
|| files.find(f => f === `${name}.node`)
}
module.exports = { find, load, maybeLoad }