|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Packaging helper: copy a better-sqlite3 native addon that matches the |
| 4 | + * packaged bridge runtime, not necessarily the host Node.js runtime. |
| 5 | + * |
| 6 | + * pkg 5.8.x embeds a Node 18.5.0 runtime for this project, so a host-built |
| 7 | + * Node 22 addon will load in development but fail inside the packaged bridge |
| 8 | + * with a NODE_MODULE_VERSION mismatch. |
| 9 | + */ |
| 10 | + |
| 11 | +const { execFileSync } = require("child_process"); |
| 12 | +const fs = require("fs"); |
| 13 | +const os = require("os"); |
| 14 | +const path = require("path"); |
| 15 | + |
| 16 | +const PKG_RUNTIME = { |
| 17 | + nodeVersion: "18.5.0", |
| 18 | + nodeModules: "108", |
| 19 | + platform: process.platform, |
| 20 | + arch: process.arch, |
| 21 | +}; |
| 22 | + |
| 23 | +function findFirstExisting(candidates) { |
| 24 | + return candidates.find((candidate) => fs.existsSync(candidate)); |
| 25 | +} |
| 26 | + |
| 27 | +function findFirstNodeBinary(rootDir) { |
| 28 | + const queue = [rootDir]; |
| 29 | + |
| 30 | + while (queue.length > 0) { |
| 31 | + const currentDir = queue.shift(); |
| 32 | + const entries = fs.readdirSync(currentDir, { withFileTypes: true }); |
| 33 | + |
| 34 | + for (const entry of entries) { |
| 35 | + const fullPath = path.join(currentDir, entry.name); |
| 36 | + if (entry.isDirectory()) { |
| 37 | + queue.push(fullPath); |
| 38 | + continue; |
| 39 | + } |
| 40 | + if (entry.isFile() && fullPath.endsWith(".node")) { |
| 41 | + return fullPath; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return undefined; |
| 47 | +} |
| 48 | + |
| 49 | +function downloadPkgRuntimeBinary(betterSqlite3Dir) { |
| 50 | + const packageJsonPath = path.join(betterSqlite3Dir, "package.json"); |
| 51 | + const tempDir = fs.mkdtempSync( |
| 52 | + path.join(os.tmpdir(), "relwave-better-sqlite3-"), |
| 53 | + ); |
| 54 | + const prebuildInstallBin = require.resolve("prebuild-install/bin.js", { |
| 55 | + paths: [betterSqlite3Dir], |
| 56 | + }); |
| 57 | + |
| 58 | + fs.copyFileSync(packageJsonPath, path.join(tempDir, "package.json")); |
| 59 | + |
| 60 | + try { |
| 61 | + execFileSync( |
| 62 | + process.execPath, |
| 63 | + [ |
| 64 | + prebuildInstallBin, |
| 65 | + "--target", |
| 66 | + PKG_RUNTIME.nodeVersion, |
| 67 | + "--runtime", |
| 68 | + "node", |
| 69 | + "--platform", |
| 70 | + PKG_RUNTIME.platform, |
| 71 | + "--arch", |
| 72 | + PKG_RUNTIME.arch, |
| 73 | + "--path", |
| 74 | + tempDir, |
| 75 | + ], |
| 76 | + { |
| 77 | + cwd: betterSqlite3Dir, |
| 78 | + stdio: "inherit", |
| 79 | + }, |
| 80 | + ); |
| 81 | + } catch (error) { |
| 82 | + console.error( |
| 83 | + "ERROR: Failed to fetch a better-sqlite3 binary for the packaged bridge runtime.", |
| 84 | + ); |
| 85 | + console.error( |
| 86 | + ` Packaged bridge runtime: Node ${PKG_RUNTIME.nodeVersion} (NODE_MODULE_VERSION ${PKG_RUNTIME.nodeModules})`, |
| 87 | + ); |
| 88 | + console.error( |
| 89 | + ` Current build runtime: ${process.version} (NODE_MODULE_VERSION ${process.versions.modules})`, |
| 90 | + ); |
| 91 | + console.error( |
| 92 | + " Re-run the build with internet access, or rebuild better-sqlite3 for Node 18 before packaging.", |
| 93 | + ); |
| 94 | + process.exit(error.status || 1); |
| 95 | + } |
| 96 | + |
| 97 | + const downloadedBinary = findFirstNodeBinary(tempDir); |
| 98 | + if (!downloadedBinary) { |
| 99 | + console.error( |
| 100 | + "ERROR: prebuild-install completed, but no better_sqlite3.node was extracted.", |
| 101 | + ); |
| 102 | + process.exit(1); |
| 103 | + } |
| 104 | + |
| 105 | + return downloadedBinary; |
| 106 | +} |
| 107 | + |
| 108 | +function resolveNativeBinarySource(betterSqlite3Dir) { |
| 109 | + const runtimeSpecificCandidates = [ |
| 110 | + path.join( |
| 111 | + betterSqlite3Dir, |
| 112 | + "lib", |
| 113 | + "binding", |
| 114 | + `node-v${PKG_RUNTIME.nodeModules}-${PKG_RUNTIME.platform}-${PKG_RUNTIME.arch}`, |
| 115 | + "better_sqlite3.node", |
| 116 | + ), |
| 117 | + path.join( |
| 118 | + betterSqlite3Dir, |
| 119 | + "compiled", |
| 120 | + PKG_RUNTIME.nodeVersion, |
| 121 | + PKG_RUNTIME.platform, |
| 122 | + PKG_RUNTIME.arch, |
| 123 | + "better_sqlite3.node", |
| 124 | + ), |
| 125 | + ]; |
| 126 | + |
| 127 | + const runtimeSpecificBinary = findFirstExisting(runtimeSpecificCandidates); |
| 128 | + if (runtimeSpecificBinary) { |
| 129 | + return runtimeSpecificBinary; |
| 130 | + } |
| 131 | + |
| 132 | + if (process.versions.modules === PKG_RUNTIME.nodeModules) { |
| 133 | + const localBuildBinary = findFirstExisting([ |
| 134 | + path.join(betterSqlite3Dir, "build", "Release", "better_sqlite3.node"), |
| 135 | + path.join(betterSqlite3Dir, "build", "Debug", "better_sqlite3.node"), |
| 136 | + ]); |
| 137 | + |
| 138 | + if (localBuildBinary) { |
| 139 | + return localBuildBinary; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return downloadPkgRuntimeBinary(betterSqlite3Dir); |
| 144 | +} |
| 145 | + |
| 146 | +const betterSqlite3Pkg = require.resolve("better-sqlite3/package.json"); |
| 147 | +const betterSqlite3Dir = path.dirname(betterSqlite3Pkg); |
| 148 | +const src = resolveNativeBinarySource(betterSqlite3Dir); |
| 149 | + |
| 150 | +if (!src || !fs.existsSync(src)) { |
| 151 | + console.error("ERROR: Could not resolve better_sqlite3.node for packaging."); |
| 152 | + process.exit(1); |
| 153 | +} |
| 154 | + |
| 155 | +const repoRoot = path.resolve(__dirname, "..", ".."); |
| 156 | +const dest = path.join( |
| 157 | + repoRoot, |
| 158 | + "src-tauri", |
| 159 | + "resources", |
| 160 | + "better_sqlite3.node", |
| 161 | +); |
| 162 | + |
| 163 | +fs.mkdirSync(path.dirname(dest), { recursive: true }); |
| 164 | +fs.copyFileSync(src, dest); |
| 165 | +console.log("Copied better_sqlite3.node"); |
| 166 | +console.log(" from:", src); |
| 167 | +console.log(" to: ", dest); |
0 commit comments