mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 10:02:59 +08:00
fix(desktop/esbuild): rebuild not working due to lack of package.json
This commit is contained in:
parent
6f8bf58456
commit
2da3d9b1ed
@ -8,23 +8,47 @@
|
||||
|
||||
import { join, resolve } from "path";
|
||||
import { rebuild } from "@electron/rebuild"
|
||||
import { readFileSync } from "fs";
|
||||
import { readFileSync, rmSync, writeFileSync } from "fs";
|
||||
|
||||
function getElectronVersion(distDir: string) {
|
||||
if (process.argv[3]) {
|
||||
return process.argv[3];
|
||||
}
|
||||
const nativeDependencies = [
|
||||
"better-sqlite3"
|
||||
];
|
||||
|
||||
const packageJsonPath = join(distDir, "package.json");
|
||||
function parsePackageJson(distDir: string) {
|
||||
const packageJsonPath = join(distDir, "../package.json");
|
||||
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) {
|
||||
console.error(`Unable to retrieve Electron version in '${resolve(packageJsonPath)}'.`);
|
||||
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() {
|
||||
@ -34,9 +58,13 @@ function main() {
|
||||
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}...`);
|
||||
|
||||
try {
|
||||
rebuild({
|
||||
// We force the project root path to avoid electron-rebuild from rebuilding the monorepo-level dependency and breaking the server.
|
||||
projectRootPath: distDir,
|
||||
@ -44,6 +72,9 @@ function main() {
|
||||
force: true,
|
||||
electronVersion,
|
||||
});
|
||||
} finally {
|
||||
rmSync(packageJsonPath);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
|
Loading…
x
Reference in New Issue
Block a user