2022-07-02 10:04:34 +12:00
|
|
|
const os = require("os")
|
|
|
|
const { join } = require("path")
|
|
|
|
const fs = require("fs")
|
|
|
|
const PREBUILDS = "prebuilds"
|
|
|
|
const ARCH = `${os.platform()}-${os.arch()}`
|
|
|
|
const PREBUILD_DIR = join(process.execPath, "..", PREBUILDS, ARCH)
|
|
|
|
|
2022-08-17 03:27:03 +12:00
|
|
|
// running as built CLI pkg bundle
|
|
|
|
if (!process.argv[0].includes("node")) {
|
|
|
|
checkForBinaries()
|
|
|
|
}
|
2022-07-02 10:04:34 +12:00
|
|
|
|
|
|
|
function checkForBinaries() {
|
2022-07-07 04:42:54 +12:00
|
|
|
const readDir = join(__filename, "..", "..", PREBUILDS, ARCH)
|
|
|
|
if (fs.existsSync(PREBUILD_DIR) || !fs.existsSync(readDir)) {
|
2022-07-02 10:04:34 +12:00
|
|
|
return
|
|
|
|
}
|
|
|
|
const natives = fs.readdirSync(readDir)
|
|
|
|
if (fs.existsSync(readDir)) {
|
|
|
|
fs.mkdirSync(PREBUILD_DIR, { recursive: true })
|
|
|
|
for (let native of natives) {
|
|
|
|
const filename = `${native.split(".fake")[0]}.node`
|
|
|
|
fs.cpSync(join(readDir, native), join(PREBUILD_DIR, filename))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-07 04:42:54 +12:00
|
|
|
function cleanup() {
|
2022-07-02 10:04:34 +12:00
|
|
|
if (fs.existsSync(PREBUILD_DIR)) {
|
|
|
|
fs.rmSync(PREBUILD_DIR, { recursive: true })
|
|
|
|
}
|
2022-07-07 04:42:54 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
const events = ["exit", "SIGINT", "SIGUSR1", "SIGUSR2", "uncaughtException"]
|
|
|
|
events.forEach(event => {
|
|
|
|
process.on(event, cleanup)
|
2022-07-02 10:04:34 +12:00
|
|
|
})
|