experiments in a post-browser web
at main 44 lines 1.3 kB view raw
1#!/usr/bin/env node 2/** 3 * Ensure native modules (better-sqlite3) are compiled for Electron's Node ABI. 4 * 5 * Fast path: tries to require() better-sqlite3 under Electron's Node runtime. 6 * Only runs electron-rebuild if the module fails to load (ABI mismatch). 7 */ 8 9import { execSync } from 'child_process'; 10import { dirname, join } from 'path'; 11import { fileURLToPath } from 'url'; 12 13const __dirname = dirname(fileURLToPath(import.meta.url)); 14const ROOT = join(__dirname, '..'); 15const MODULE_PATH = join(ROOT, 'node_modules/better-sqlite3'); 16 17function canLoad() { 18 try { 19 const result = execSync( 20 `ELECTRON_RUN_AS_NODE=1 npx electron -e "try { require('${MODULE_PATH}'); process.stdout.write('ok') } catch(e) { process.stdout.write('fail') }"`, 21 { cwd: ROOT, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] } 22 ); 23 return result.trim() === 'ok'; 24 } catch { 25 return false; 26 } 27} 28 29if (canLoad()) { 30 console.log('[check-native-modules] better-sqlite3 loads in Electron — skip rebuild'); 31 process.exit(0); 32} 33 34console.log('[check-native-modules] better-sqlite3 ABI mismatch — rebuilding...'); 35 36try { 37 execSync('npx electron-rebuild -f -w better-sqlite3', { 38 cwd: ROOT, 39 stdio: 'inherit' 40 }); 41} catch (e) { 42 console.error('[check-native-modules] Failed to rebuild:', e.message); 43 process.exit(1); 44}