2025-03-10 16:20:48 +02:00
|
|
|
import fs from "fs/promises";
|
|
|
|
import fsExtra from "fs-extra";
|
|
|
|
import path from "path";
|
2025-03-10 16:37:39 +02:00
|
|
|
import type NoteMeta from "./src/services/meta/note_meta.js";
|
|
|
|
import type { NoteMetaFile } from "./src/services/meta/note_meta.js";
|
2025-03-10 17:04:17 +02:00
|
|
|
import cls from "./src/services/cls.js";
|
|
|
|
import { initializeTranslations } from "./src/services/i18n.js";
|
2025-03-10 16:20:48 +02:00
|
|
|
|
2025-03-10 17:41:13 +02:00
|
|
|
const NOTE_ID_USER_GUIDE = "_help_user_guide";
|
2025-03-10 16:37:39 +02:00
|
|
|
const destRootPath = path.join("src", "public", "app", "doc_notes", "en", "User Guide");
|
2025-03-10 16:20:48 +02:00
|
|
|
|
2025-02-28 22:02:29 +02:00
|
|
|
async function startElectron() {
|
|
|
|
await import("./electron-main.js");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2025-03-10 17:04:17 +02:00
|
|
|
await initializeTranslations();
|
2025-03-10 17:08:40 +02:00
|
|
|
await initializeDatabase();
|
2025-03-10 17:41:13 +02:00
|
|
|
cls.init(() => {
|
|
|
|
importData();
|
|
|
|
});
|
2025-03-10 17:04:17 +02:00
|
|
|
|
2025-02-28 22:02:29 +02:00
|
|
|
await startElectron();
|
2025-03-10 17:04:17 +02:00
|
|
|
// await exportData();
|
|
|
|
}
|
|
|
|
|
2025-03-10 17:08:40 +02:00
|
|
|
async function initializeDatabase() {
|
2025-03-10 17:04:17 +02:00
|
|
|
const sqlInit = (await import("./src/services/sql_init.js")).default;
|
|
|
|
|
|
|
|
cls.init(() => {
|
2025-03-10 17:08:40 +02:00
|
|
|
if (!sqlInit.isDbInitialized()) {
|
|
|
|
sqlInit.createInitialDatabase();
|
|
|
|
}
|
2025-03-10 17:04:17 +02:00
|
|
|
});
|
2025-03-10 16:20:48 +02:00
|
|
|
}
|
|
|
|
|
2025-03-10 17:41:13 +02:00
|
|
|
async function importData() {
|
|
|
|
const beccaLoader = ((await import("./src/becca/becca_loader.js")).default);
|
|
|
|
const notes = ((await import("./src/services/notes.js")).default);
|
|
|
|
beccaLoader.load();
|
|
|
|
|
|
|
|
const becca = ((await import("./src/becca/becca.js")).default);
|
|
|
|
|
|
|
|
notes.createNewNoteWithTarget("into", "none_root", {
|
|
|
|
parentNoteId: "root",
|
|
|
|
noteId: NOTE_ID_USER_GUIDE,
|
|
|
|
title: "User Guide",
|
|
|
|
content: "The sub-children of this note are automatically synced.",
|
|
|
|
type: "text"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-10 16:20:48 +02:00
|
|
|
async function exportData() {
|
|
|
|
const zipFilePath = "output.zip";
|
|
|
|
|
2025-03-10 16:31:44 +02:00
|
|
|
const deferred = (await import("./src/services/utils.js")).deferred;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await fsExtra.remove(destRootPath);
|
|
|
|
await fsExtra.mkdir(destRootPath);
|
|
|
|
|
|
|
|
// First export as zip.
|
|
|
|
const { exportToZipFile } = (await import("./src/services/export/zip.js")).default;
|
|
|
|
await exportToZipFile(NOTE_ID_USER_GUIDE, "html", zipFilePath);
|
|
|
|
|
|
|
|
const promise = deferred<void>()
|
|
|
|
setTimeout(async () => {
|
|
|
|
// Then extract the zip.
|
|
|
|
const { readZipFile, readContent } = (await import("./src/services/import/zip.js"));
|
|
|
|
await readZipFile(await fs.readFile(zipFilePath), async (zip, entry) => {
|
|
|
|
// We ignore directories since they can appear out of order anyway.
|
|
|
|
if (!entry.fileName.endsWith("/")) {
|
|
|
|
const destPath = path.join(destRootPath, 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;
|
|
|
|
} finally {
|
|
|
|
if (await fsExtra.exists(zipFilePath)) {
|
|
|
|
await fsExtra.rm(zipFilePath);
|
|
|
|
}
|
|
|
|
}
|
2025-03-10 16:37:39 +02:00
|
|
|
|
|
|
|
await cleanUpMeta();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanUpMeta() {
|
|
|
|
const metaPath = path.join(destRootPath, "!!!meta.json");
|
|
|
|
const meta = JSON.parse(await fs.readFile(metaPath, "utf-8")) as NoteMetaFile;
|
|
|
|
for (const file of meta.files) {
|
|
|
|
traverse(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function traverse(el: NoteMeta) {
|
|
|
|
for (const child of el.children || []) {
|
|
|
|
traverse(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
el.isExpanded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
await fs.writeFile(metaPath, JSON.stringify(meta, null, 4));
|
2025-02-28 22:02:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await main();
|