2025-09-03 18:16:03 +03:00
import { join , resolve } from "path" ;
import { cpSync , 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 } ) ;
}
mkdirSync ( destPath ) ;
cpSync ( join ( workspaceRoot , "node_modules/better-sqlite3" ) , destPath , { recursive : true , dereference : true } ) ;
}
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-03 18:16:03 +03:00
console . log ( ` Rebuilding ${ projectRoot } with ${ electronVersion } ... ` ) ;
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 ,
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 ) ;
}