mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 21:19:01 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			81 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
/**
 | 
						|
 * @module
 | 
						|
 *
 | 
						|
 * This script is used internally by the `rebuild-deps` target of the `desktop`. Normally we could use
 | 
						|
 * `electron-rebuild` CLI directly, but it would rebuild the monorepo-level dependencies and breaks
 | 
						|
 * the server build (and it doesn't expose a CLI option to override this).
 | 
						|
 */
 | 
						|
 | 
						|
import { join, resolve } from "path";
 | 
						|
import { rebuild } from "@electron/rebuild"
 | 
						|
import { readFileSync, rmSync, writeFileSync } from "fs";
 | 
						|
 | 
						|
const nativeDependencies = [
 | 
						|
    "better-sqlite3"
 | 
						|
];
 | 
						|
 | 
						|
function parsePackageJson(distDir: string) {
 | 
						|
    const packageJsonPath = join(distDir, "../package.json");
 | 
						|
    const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
 | 
						|
    let electronVersion: string;
 | 
						|
 | 
						|
    if (process.argv[3]) {
 | 
						|
        electronVersion = process.argv[3];
 | 
						|
    } else {
 | 
						|
        electronVersion = packageJson?.devDependencies?.electron ?? packageJson?.dependencies?.electron;
 | 
						|
        if (!electronVersion) {
 | 
						|
            console.error(`Unable to retrieve Electron version in '${resolve(packageJsonPath)}'.`);
 | 
						|
            process.exit(3);
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
    return {
 | 
						|
        electronVersion,
 | 
						|
        dependencies: packageJson?.dependencies ?? []
 | 
						|
    };
 | 
						|
}
 | 
						|
 | 
						|
function createFakePackageJson(distPath: string, dependencies: Record<string, string>) {
 | 
						|
    const finalDependencies = {};
 | 
						|
    for (const dep of nativeDependencies) {
 | 
						|
        finalDependencies[dep] = dependencies[dep];
 | 
						|
    }
 | 
						|
 | 
						|
    const fakePackageJson = {
 | 
						|
        name: "trilium",
 | 
						|
        version: "1.0.0",
 | 
						|
        main: "index.js",
 | 
						|
        dependencies: finalDependencies,
 | 
						|
        devDependencies: {},
 | 
						|
    };
 | 
						|
    writeFileSync(distPath, JSON.stringify(fakePackageJson, null, 2), "utf-8");
 | 
						|
}
 | 
						|
 | 
						|
function main() {
 | 
						|
    const distDir = resolve(process.argv[2]);
 | 
						|
    if (!distDir) {
 | 
						|
        console.error("Missing root dir as argument.");
 | 
						|
        process.exit(1);
 | 
						|
    }
 | 
						|
 | 
						|
    const { electronVersion, dependencies } = parsePackageJson(distDir);
 | 
						|
    const packageJsonPath = join(distDir, "package.json");
 | 
						|
    createFakePackageJson(packageJsonPath, dependencies);
 | 
						|
 | 
						|
    console.log(`Rebuilding ${distDir} with version ${electronVersion}...`);
 | 
						|
 | 
						|
    try {
 | 
						|
        rebuild({
 | 
						|
            // We force the project root path to avoid electron-rebuild from rebuilding the monorepo-level dependency and breaking the server.
 | 
						|
            projectRootPath: distDir,
 | 
						|
            buildPath: distDir,
 | 
						|
            force: true,
 | 
						|
            electronVersion,
 | 
						|
        });
 | 
						|
    } finally {
 | 
						|
        rmSync(packageJsonPath);
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
main();
 |