|
1 | 1 | import path from "path" |
2 | 2 | import fs from "fs" |
3 | 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).sort((a, b) => { |
13 | | - return Number.parseInt(b, 10) - Number.parseInt(a, 10) |
14 | | -}) |
15 | | - |
16 | | -let addon: undefined | any |
17 | | -// try each available addon ABI |
18 | | -for (const addOnAbiDir of addOnAbiDirs) { |
19 | | - const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node") |
| 4 | +function findAddon(): any | undefined { |
20 | 5 | try { |
21 | | - addon = require(addonPath) |
22 | | - } catch (err) { |
23 | | - console.error( |
24 | | - `Failed to load addon at ${addonPath}: ${err}\nTrying others...`, |
| 6 | + const addonParentDir = path.join( |
| 7 | + __dirname, |
| 8 | + "..", |
| 9 | + "build", |
| 10 | + process.platform, |
| 11 | + process.arch, |
| 12 | + "node", |
25 | 13 | ) |
26 | | - } |
27 | | -} |
| 14 | + const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => { |
| 15 | + return Number.parseInt(b, 10) - Number.parseInt(a, 10) |
| 16 | + }) |
28 | 17 |
|
29 | | -if (addon === undefined) { |
30 | | - throw new Error( |
31 | | - `No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`, |
32 | | - ) |
| 18 | + // try each available addon ABI |
| 19 | + let addon: undefined | any |
| 20 | + for (const addOnAbiDir of addOnAbiDirs) { |
| 21 | + const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node") |
| 22 | + try { |
| 23 | + addon = require(addonPath) |
| 24 | + break |
| 25 | + } catch (err) { |
| 26 | + console.error( |
| 27 | + `Failed to load addon at ${addonPath}: ${err}\nTrying others...`, |
| 28 | + ) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if (addon === undefined) { |
| 33 | + throw new Error( |
| 34 | + `No compatible zeromq.js addon found in ${addonParentDir} folder`, |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + return addon |
| 39 | + } catch (err) { |
| 40 | + throw new Error(`Failed to load zeromq.js addon.node: ${err}`) |
| 41 | + } |
33 | 42 | } |
34 | 43 |
|
35 | | -module.exports = addon |
| 44 | +module.exports = findAddon() |
0 commit comments