2025-09-03 18:16:03 +03:00
import { join , resolve } from "path" ;
2025-09-28 00:07:30 +03:00
import { cpSync , exists , existsSync , mkdirSync , readFileSync , rmSync } from "fs" ;
2025-09-02 10:45:42 +03:00
import { execSync } from "child_process" ;
import { rebuild } from "@electron/rebuild"
2025-09-03 18:16:03 +03:00
import { getElectronPath , isNixOS } from "./utils.mjs" ;
2025-09-02 10:45:42 +03:00
2025-09-03 18:16:03 +03:00
const workspaceRoot = join ( import . meta . dirname , ".." ) ;
2025-09-02 10:45:42 +03:00
2025-09-03 18:16:03 +03:00
function copyNativeDependencies ( projectRoot : string ) {
const destPath = join ( projectRoot , "node_modules/better-sqlite3" ) ;
2025-09-02 11:50:53 +03:00
2025-09-02 10:45:42 +03:00
if ( existsSync ( destPath ) ) {
rmSync ( destPath , { recursive : true } ) ;
}
2025-09-28 00:07:30 +03:00
mkdirSync ( destPath , { recursive : true } ) ;
const sourcePath = join ( workspaceRoot , "node_modules/better-sqlite3" ) ;
if ( ! existsSync ( sourcePath ) ) {
console . warn ( "Nothing to rebuild as source path is missing:" , sourcePath ) ;
console . info ( "For CI builds with filtered package installs, this is normal. For normal development, it's not." ) ;
process . exit ( 0 ) ;
}
cpSync ( sourcePath , destPath , { recursive : true , dereference : true } ) ;
2025-09-02 10:45:42 +03:00
}
2025-09-03 18:16:03 +03:00
function rebuildNativeDependencies ( projectRoot : string ) {
const electronVersion = determineElectronVersion ( projectRoot ) ;
2025-09-02 10:45:42 +03:00
if ( ! electronVersion ) {
console . error ( "Unable to determine Electron version." ) ;
process . exit ( 1 ) ;
}
2025-09-16 21:29:16 +08:00
const targetArch = process . env . TARGET_ARCH || process . arch ;
console . log ( ` Rebuilding ${ projectRoot } with ${ electronVersion } for ${ targetArch } ... ` ) ;
2025-09-02 10:45:42 +03:00
2025-09-03 18:16:03 +03:00
const resolvedPath = resolve ( projectRoot ) ;
2025-09-02 10:45:42 +03:00
rebuild ( {
2025-09-03 18:16:03 +03:00
projectRootPath : resolvedPath ,
buildPath : resolvedPath ,
electronVersion ,
2025-09-16 21:29:16 +08:00
arch : targetArch ,
2025-09-03 18:16:03 +03:00
force : true
2025-09-02 10:45:42 +03:00
} ) ;
}
2025-09-03 18:16:03 +03:00
function determineElectronVersion ( projectRoot : string ) {
const packageJson = JSON . parse ( readFileSync ( join ( projectRoot , "package.json" ) , "utf-8" ) ) ;
2025-09-02 10:45:42 +03:00
if ( isNixOS ( ) ) {
console . log ( "Detected NixOS, reading Electron version from PATH" ) ;
try {
2025-09-02 19:56:27 +03:00
return execSync ( ` ${ getElectronPath ( ) } --version ` , { } ) . toString ( "utf-8" ) ;
2025-09-02 10:45:42 +03:00
} catch ( e ) {
console . error ( "Got error while trying to read the Electron version from shell. Make sure that an Electron version is in the PATH (e.g. `nix-shell -p electron`)" ) ;
process . exit ( 1 ) ;
}
} else {
console . log ( "Using Electron version from package.json" ) ;
2025-09-02 17:37:57 +03:00
return packageJson . devDependencies . electron ;
2025-09-02 10:45:42 +03:00
}
}
2025-09-03 18:16:03 +03:00
for ( const projectRoot of [ "apps/desktop" , "apps/edit-docs" ] ) {
copyNativeDependencies ( projectRoot ) ;
rebuildNativeDependencies ( projectRoot ) ;
}