2025-04-21 23:18:45 +03:00
|
|
|
import cls from "@triliumnext/server/src/services/cls.js";
|
2025-03-30 21:37:10 +03:00
|
|
|
import fs from "fs/promises";
|
|
|
|
import fsExtra from "fs-extra";
|
|
|
|
import path from "path";
|
2025-05-27 18:32:00 +03:00
|
|
|
import electron from "electron";
|
|
|
|
import { deferred, type DeferredPromise } from "@triliumnext/server/src/services/utils.js";
|
|
|
|
import windowService from "@triliumnext/server/src/services/window.js";
|
2025-03-30 21:31:35 +03:00
|
|
|
|
2025-04-11 14:02:35 +03:00
|
|
|
export function initializeDatabase(skipDemoDb: boolean) {
|
2025-04-04 20:01:28 +03:00
|
|
|
return new Promise<void>(async (resolve) => {
|
2025-04-21 23:18:45 +03:00
|
|
|
const sqlInit = (await import("@triliumnext/server/src/services/sql_init.js")).default;
|
2025-04-04 20:01:28 +03:00
|
|
|
cls.init(async () => {
|
|
|
|
if (!sqlInit.isDbInitialized()) {
|
2025-04-11 14:02:35 +03:00
|
|
|
await sqlInit.createInitialDatabase(skipDemoDb);
|
2025-04-04 20:01:28 +03:00
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
2025-03-30 21:31:35 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-05-27 18:32:00 +03:00
|
|
|
/**
|
|
|
|
* Electron has a behaviour in which the "ready" event must have a listener attached before it gets to initialize.
|
|
|
|
* If async tasks are awaited before the "ready" event is bound, then the window will never shown.
|
|
|
|
* This method works around by creating a deferred promise. It will immediately bind to the "ready" event and wait for that promise to be resolved externally.
|
|
|
|
*
|
|
|
|
* @param callback a method to be called after the server and Electron is initialized.
|
|
|
|
* @returns the deferred promise that must be resolved externally before the Electron app is started.
|
|
|
|
*/
|
|
|
|
export function startElectron(callback: () => void): DeferredPromise<void> {
|
|
|
|
const initializedPromise = deferred<void>();
|
|
|
|
electron.app.on("ready", async () => {
|
|
|
|
await initializedPromise;
|
|
|
|
|
|
|
|
console.log("Electron is ready!");
|
|
|
|
|
|
|
|
// Start the server.
|
|
|
|
await import("@triliumnext/server/src/main.js");
|
|
|
|
|
|
|
|
// Create the main window.
|
|
|
|
await windowService.createMainWindow(electron.app);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
return initializedPromise;
|
2025-03-30 21:31:35 +03:00
|
|
|
}
|
|
|
|
|
2025-04-12 01:13:45 +03:00
|
|
|
export async function extractZip(zipFilePath: string, outputPath: string, ignoredFiles?: Set<string>) {
|
2025-04-21 23:18:45 +03:00
|
|
|
const deferred = (await import("@triliumnext/server/src/services/utils.js")).deferred;
|
2025-03-30 21:37:10 +03:00
|
|
|
|
|
|
|
const promise = deferred<void>()
|
|
|
|
setTimeout(async () => {
|
|
|
|
// Then extract the zip.
|
2025-04-21 23:18:45 +03:00
|
|
|
const { readZipFile, readContent } = (await import("@triliumnext/server/src/services/import/zip.js"));
|
2025-03-30 21:37:10 +03:00
|
|
|
await readZipFile(await fs.readFile(zipFilePath), async (zip, entry) => {
|
|
|
|
// We ignore directories since they can appear out of order anyway.
|
2025-04-12 01:13:45 +03:00
|
|
|
if (!entry.fileName.endsWith("/") && !ignoredFiles?.has(entry.fileName)) {
|
2025-03-30 21:37:10 +03:00
|
|
|
const destPath = path.join(outputPath, entry.fileName);
|
|
|
|
const fileContent = await readContent(zip, entry);
|
|
|
|
|
|
|
|
await fsExtra.mkdirs(path.dirname(destPath));
|
|
|
|
await fs.writeFile(destPath, fileContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
zip.readEntry();
|
|
|
|
});
|
|
|
|
promise.resolve();
|
|
|
|
}, 1000);
|
|
|
|
await promise;
|
|
|
|
}
|