Notes/apps/edit-docs/src/edit-demo.ts

63 lines
2.3 KiB
TypeScript
Raw Normal View History

import { extractZip, importData, initializeDatabase, startElectron } from "./utils.js";
2025-04-21 23:18:45 +03:00
import { initializeTranslations } from "@triliumnext/server/src/services/i18n.js";
import debounce from "@triliumnext/client/src/services/debounce.js";
2025-03-30 23:09:17 +03:00
import fs from "fs/promises";
2025-05-27 18:22:10 +03:00
import { join } from "path";
import cls from "@triliumnext/server/src/services/cls.js";
// Paths are relative to apps/edit-docs/dist.
2025-05-27 18:22:10 +03:00
const DEMO_ZIP_PATH = join(__dirname, "../../server/src/assets/db/demo.zip");
const DEMO_ZIP_DIR_PATH = join(__dirname, "../demo");
async function main() {
const initializedPromise = startElectron(() => {
// Wait for the import to be finished and the application to be loaded before we listen to changes.
setTimeout(() => registerHandlers(), 10_000);
});
await initializeTranslations();
await initializeDatabase(true);
cls.init(async () => {
await importData(DEMO_ZIP_DIR_PATH);
setOptions();
initializedPromise.resolve();
});
initializedPromise.resolve();
}
async function setOptions() {
const optionsService = (await import("@triliumnext/server/src/services/options.js")).default;
optionsService.setOption("eraseUnusedAttachmentsAfterSeconds", 10);
optionsService.setOption("eraseUnusedAttachmentsAfterTimeScale", 60);
optionsService.setOption("compressImages", "false");
}
async function registerHandlers() {
2025-04-21 23:18:45 +03:00
const events = (await import("@triliumnext/server/src/services/events.js")).default;
const eraseService = (await import("@triliumnext/server/src/services/erase.js")).default;
const debouncer = debounce(async () => {
console.log("Exporting data");
eraseService.eraseUnusedAttachmentsNow();
await exportData();
2025-03-30 23:09:17 +03:00
await fs.rmdir(DEMO_ZIP_DIR_PATH, { recursive: true }).catch(() => {});
await extractZip(DEMO_ZIP_PATH, DEMO_ZIP_DIR_PATH);
}, 10_000);
events.subscribe(events.ENTITY_CHANGED, async (e) => {
if (e.entityName === "options") {
return;
}
console.log("Got entity changed ", e);
debouncer();
});
}
async function exportData() {
2025-04-21 23:18:45 +03:00
const { exportToZipFile } = (await import("@triliumnext/server/src/services/export/zip.js")).default;
2025-03-30 22:26:31 +03:00
await exportToZipFile("root", "html", DEMO_ZIP_PATH);
}
2025-05-25 11:47:03 +03:00
main();