mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	fix(desktop/esbuild): rebuild not working due to lack of package.json
This commit is contained in:
		| @@ -8,23 +8,47 @@ | |||||||
|  |  | ||||||
| import { join, resolve } from "path"; | import { join, resolve } from "path"; | ||||||
| import { rebuild } from "@electron/rebuild" | import { rebuild } from "@electron/rebuild" | ||||||
| import { readFileSync } from "fs"; | import { readFileSync, rmSync, writeFileSync } from "fs"; | ||||||
|  |  | ||||||
| function getElectronVersion(distDir: string) { | const nativeDependencies = [ | ||||||
|     if (process.argv[3]) { |     "better-sqlite3" | ||||||
|         return process.argv[3]; | ]; | ||||||
|     } |  | ||||||
|  |  | ||||||
|     const packageJsonPath = join(distDir, "package.json"); | function parsePackageJson(distDir: string) { | ||||||
|  |     const packageJsonPath = join(distDir, "../package.json"); | ||||||
|     const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); |     const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8")); | ||||||
|  |     let electronVersion: string; | ||||||
|  |  | ||||||
|     const electronVersion = packageJson?.devDependencies?.electron ?? packageJson?.dependencies?.electron; |     if (process.argv[3]) { | ||||||
|  |         electronVersion = process.argv[3]; | ||||||
|  |     } else { | ||||||
|  |         electronVersion = packageJson?.devDependencies?.electron ?? packageJson?.dependencies?.electron; | ||||||
|         if (!electronVersion) { |         if (!electronVersion) { | ||||||
|             console.error(`Unable to retrieve Electron version in '${resolve(packageJsonPath)}'.`); |             console.error(`Unable to retrieve Electron version in '${resolve(packageJsonPath)}'.`); | ||||||
|             process.exit(3); |             process.exit(3); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
|      |      | ||||||
|     return electronVersion; |     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() { | function main() { | ||||||
| @@ -34,9 +58,13 @@ function main() { | |||||||
|         process.exit(1); |         process.exit(1); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const electronVersion = getElectronVersion(distDir); |     const { electronVersion, dependencies } = parsePackageJson(distDir); | ||||||
|  |     const packageJsonPath = join(distDir, "package.json"); | ||||||
|  |     createFakePackageJson(packageJsonPath, dependencies); | ||||||
|  |  | ||||||
|     console.log(`Rebuilding ${distDir} with version ${electronVersion}...`); |     console.log(`Rebuilding ${distDir} with version ${electronVersion}...`); | ||||||
|  |  | ||||||
|  |     try { | ||||||
|         rebuild({ |         rebuild({ | ||||||
|             // We force the project root path to avoid electron-rebuild from rebuilding the monorepo-level dependency and breaking the server. |             // We force the project root path to avoid electron-rebuild from rebuilding the monorepo-level dependency and breaking the server. | ||||||
|             projectRootPath: distDir, |             projectRootPath: distDir, | ||||||
| @@ -44,6 +72,9 @@ function main() { | |||||||
|             force: true, |             force: true, | ||||||
|             electronVersion, |             electronVersion, | ||||||
|         }); |         }); | ||||||
|  |     } finally { | ||||||
|  |         rmSync(packageJsonPath); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| main(); | main(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user