Skip to content

Commit

Permalink
fix: add postinstall script for testing addons
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Oct 20, 2024
1 parent 491d38d commit 80ee865
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 90 deletions.
8 changes: 0 additions & 8 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,8 @@
"mochaExplorer.nodeArgv": ["--expose-gc"],
"mochaExplorer.debuggerConfig": "JS-Attach",
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/Thumbs.db": true,
"**/.cache": true,
"**/script/*.js": true,
"**/script/*.mjs": true,
"**/script/*.js.map": true,
"**/script/*.mjs.map": true,
"**/script/*.d.ts": true,
"**/script/*.d.mts": true,
"**/script/*.tsbuildinfo": true
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
},
"homepage": "http://zeromq.github.io/zeromq.js/",
"dependencies": {
"@aminya/node-gyp-build": "4.8.1-aminya.1",
"cmake-ts": "^0.3.0",
"cross-env": "^7.0.3",
"node-addon-api": "^8.2.1",
Expand Down Expand Up @@ -85,6 +84,7 @@
"tsconfig.json"
],
"scripts": {
"install": "node ./script/install.js",
"prepare": "pnpm run build.js",
"clean": "shx rm -rf ./build ./prebuilds && run-p clean.lib clean.temp",
"clean.lib": "shx rm -rf ./lib/",
Expand Down
9 changes: 0 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions script/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
try {
require("../lib/load-addon.js")
} catch (error) {
const cp = require("child_process")

console.error("Failed to load ZMQ addon:", error)

// Run the build script to generate the addon.node file
console.log("Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)")
const cmakeTsPath = require.resolve("cmake-ts/build/main.js")

cp.execFileSync(
"node", [cmakeTsPath, "nativeonly"],
{ stdio: "inherit" },
)
}
59 changes: 0 additions & 59 deletions script/prebuild.mjs

This file was deleted.

29 changes: 29 additions & 0 deletions src/load-addon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import path from "path"
import fs from "fs"

const addonParentDir = path.join(
__dirname,
"..",
"build",
process.platform,
process.arch,
"node",
)
const addOnAbiDirs = fs.readdirSync(addonParentDir)

let addon: undefined | any
// try each available addon ABI
for (const addOnAbiDir of addOnAbiDirs) {
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
try {
addon = require(addonPath)
} catch (err) {
console.error(`Failed to load addon at ${addonPath}: ${err}\nTrying others...`)
}
}

if (addon === undefined) {
throw new Error(`No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`)
}

module.exports = addon
14 changes: 1 addition & 13 deletions src/native.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */

/* Declare all native C++ classes and methods in this file. */
import path from "path"
import fs from "fs"
const addonParentDir = path.join(
__dirname,
"..",
"build",
process.platform,
process.arch,
"node",
)
const addOnAbiDir = fs.readdirSync(addonParentDir)[0]
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
module.exports = require(addonPath)
module.exports = require("./load-addon")

/**
* The version of the ØMQ library the bindings were built with. Formatted as
Expand Down

0 comments on commit 80ee865

Please sign in to comment.