-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.js
46 lines (40 loc) · 1.23 KB
/
install.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
const fs = require("fs")
const path = require("path")
const { spawn } = require("child_process")
const abi = require("node-abi")
// Examples:
// win32-x64-node-abi108.node
// linux-x64-node-abi108.node
// linux-arm-node-abi108.node
function prebuildName() {
const runtime = process.release.name || "node"
const version = process.versions[runtime]
if (!version) {
throw new Error(`runtime ${runtime} is not supported`)
}
const abiv = abi.getAbi(process.versions[runtime], runtime)
return `${encodeName(process.platform)}-${encodeName(
process.arch
)}-${runtime}-abi${abiv}.node`
}
function encodeName(name) {
return name.replace(/\//g, "+")
}
// Compute the path we want to emit the binary to
const binaryPath = path.join(__dirname, "index.node")
function isBuilt() {
return fs.existsSync(binaryPath)
}
function build() {
spawn("npm", ["run", "build"], { stdio: "inherit", shell: true })
}
if (!isBuilt()) {
const platformSpecificBinaryPath = path.join(__dirname, 'bin', prebuildName())
if (fs.existsSync(platformSpecificBinaryPath)) {
fs.copyFileSync(platformSpecificBinaryPath, binaryPath)
console.log("Platform specific binary installed.")
} else {
console.log("Building from source...")
build()
}
}