Skip to content

Commit 896bf97

Browse files
committed
fix: add postinstall script for testing addons
1 parent 491d38d commit 896bf97

File tree

6 files changed

+47
-81
lines changed

6 files changed

+47
-81
lines changed

.vscode/settings.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@
44
"mochaExplorer.nodeArgv": ["--expose-gc"],
55
"mochaExplorer.debuggerConfig": "JS-Attach",
66
"files.exclude": {
7-
"**/.git": true,
87
"**/.DS_Store": true,
98
"**/Thumbs.db": true,
109
"**/.cache": true,
11-
"**/script/*.js": true,
12-
"**/script/*.mjs": true,
13-
"**/script/*.js.map": true,
14-
"**/script/*.mjs.map": true,
15-
"**/script/*.d.ts": true,
16-
"**/script/*.d.mts": true,
17-
"**/script/*.tsbuildinfo": true
1810
}
1911
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
},
2020
"homepage": "http://zeromq.github.io/zeromq.js/",
2121
"dependencies": {
22-
"@aminya/node-gyp-build": "4.8.1-aminya.1",
2322
"cmake-ts": "^0.3.0",
2423
"cross-env": "^7.0.3",
2524
"node-addon-api": "^8.2.1",
@@ -85,6 +84,7 @@
8584
"tsconfig.json"
8685
],
8786
"scripts": {
87+
"install": "node ./script/install.js",
8888
"prepare": "pnpm run build.js",
8989
"clean": "shx rm -rf ./build ./prebuilds && run-p clean.lib clean.temp",
9090
"clean.lib": "shx rm -rf ./lib/",

script/install.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
try {
2+
require("../lib/load-addon.js")
3+
} catch (error) {
4+
const cp = require("child_process")
5+
6+
console.error("Failed to load ZMQ addon:", error)
7+
8+
// Run the build script to generate the addon.node file
9+
console.log("Building addon node via cmake-ts (requires cmake, ninja, and the vcpkg dependencies)")
10+
const cmakeTsPath = require.resolve("cmake-ts/build/main.js")
11+
12+
cp.execFileSync(
13+
"node", [cmakeTsPath, "nativeonly"],
14+
{ stdio: "inherit" },
15+
)
16+
}

script/prebuild.mjs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/load-addon.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from "path"
2+
import fs from "fs"
3+
4+
const addonParentDir = path.join(
5+
__dirname,
6+
"..",
7+
"build",
8+
process.platform,
9+
process.arch,
10+
"node",
11+
)
12+
const addOnAbiDirs = fs.readdirSync(addonParentDir)
13+
14+
let addon: undefined | any
15+
// try each available addon ABI
16+
for (const addOnAbiDir of addOnAbiDirs) {
17+
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
18+
try {
19+
addon = require(addonPath)
20+
} catch (err) {
21+
console.error(`Failed to load addon at ${addonPath}: ${err}\nTrying others...`)
22+
}
23+
}
24+
25+
if (addon === undefined) {
26+
throw new Error(`No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`)
27+
}
28+
29+
module.exports = addon

src/native.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-var-requires */
22

33
/* Declare all native C++ classes and methods in this file. */
4-
import path from "path"
5-
import fs from "fs"
6-
const addonParentDir = path.join(
7-
__dirname,
8-
"..",
9-
"build",
10-
process.platform,
11-
process.arch,
12-
"node",
13-
)
14-
const addOnAbiDir = fs.readdirSync(addonParentDir)[0]
15-
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
16-
module.exports = require(addonPath)
4+
module.exports = require("./load-addon")
175

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

0 commit comments

Comments
 (0)