chore(edit-docs): change import mechanism

This commit is contained in:
Elian Doran 2025-04-11 14:02:35 +03:00
parent 3c5f5d2f76
commit 6231ac59d6
No known key found for this signature in database
3 changed files with 33 additions and 14 deletions

View File

@ -10,6 +10,7 @@ import debounce from "./src/public/app/services/debounce.js";
import { extractZip, initializeDatabase, startElectron } from "./electron-utils.js"; import { extractZip, initializeDatabase, startElectron } from "./electron-utils.js";
import cls from "./src/services/cls.js"; import cls from "./src/services/cls.js";
import type { AdvancedExportOptions } from "./src/services/export/zip.js"; import type { AdvancedExportOptions } from "./src/services/export/zip.js";
import TaskContext from "./src/services/task_context.js";
const NOTE_ID_USER_GUIDE = "pOsGYCXsbNQG"; const NOTE_ID_USER_GUIDE = "pOsGYCXsbNQG";
const markdownPath = path.join("docs", "User Guide"); const markdownPath = path.join("docs", "User Guide");
@ -17,11 +18,14 @@ const htmlPath = path.join("src", "public", "app", "doc_notes", "en", "User Guid
async function main() { async function main() {
await initializeTranslations(); await initializeTranslations();
const zipBuffer = await createImportZip(); await initializeDatabase(true);
await initializeDatabase(zipBuffer);
cls.init(async () => {
await importData(markdownPath);
setOptions();
});
await startElectron(); await startElectron();
cls.init(() => setOptions());
// Wait for the import to be finished and the application to be loaded before we listen to changes. // Wait for the import to be finished and the application to be loaded before we listen to changes.
setTimeout(() => registerHandlers(), 10_000); setTimeout(() => registerHandlers(), 10_000);
@ -34,13 +38,28 @@ async function setOptions() {
optionsService.setOption("compressImages", "false"); optionsService.setOption("compressImages", "false");
} }
async function createImportZip() { async function importData(path: string) {
const buffer = await createImportZip(path);
const importService = (await import("./src/services/import/zip.js")).default;
const context = new TaskContext("no-progress-reporting", "import", false);
const becca = (await import("./src/becca/becca.js")).default;
const rootNote = becca.getRoot();
if (!rootNote) {
throw new Error("Missing root note for import.");
}
await importService.importZip(context, buffer, rootNote, {
preserveIds: true
});
}
async function createImportZip(path: string) {
const inputFile = "input.zip"; const inputFile = "input.zip";
const archive = archiver("zip", { const archive = archiver("zip", {
zlib: { level: 0 } zlib: { level: 0 }
}); });
archive.directory(markdownPath, "/"); archive.directory(path, "/");
const outputStream = fsExtra.createWriteStream(inputFile); const outputStream = fsExtra.createWriteStream(inputFile);
archive.pipe(outputStream); archive.pipe(outputStream);

View File

@ -3,12 +3,12 @@ import fs from "fs/promises";
import fsExtra from "fs-extra"; import fsExtra from "fs-extra";
import path from "path"; import path from "path";
export function initializeDatabase(customDbBuffer?: Buffer) { export function initializeDatabase(skipDemoDb: boolean) {
return new Promise<void>(async (resolve) => { return new Promise<void>(async (resolve) => {
const sqlInit = (await import("./src/services/sql_init.js")).default; const sqlInit = (await import("./src/services/sql_init.js")).default;
cls.init(async () => { cls.init(async () => {
if (!sqlInit.isDbInitialized()) { if (!sqlInit.isDbInitialized()) {
await sqlInit.createInitialDatabase(true, customDbBuffer); await sqlInit.createInitialDatabase(skipDemoDb);
} }
resolve(); resolve();
}); });

View File

@ -67,16 +67,16 @@ async function initDbConnection() {
/** /**
* Applies the database schema, creating the necessary tables and importing the demo content. * Applies the database schema, creating the necessary tables and importing the demo content.
* *
* @param preserveIds `true` if the note IDs from the meta file should be preserved, or `false` to generate new ones (normal behaviour). * @param skipDemoDb if set to `true`, then the demo database will not be imported, resulting in an empty root note.
* @param customDbBuffer a custom database buffer to use, otherwise the default demo one is going to be used. * @throws {Error} if the database is already initialized.
*/ */
async function createInitialDatabase(preserveIds?: boolean, customDbBuffer?: Buffer) { async function createInitialDatabase(skipDemoDb?: boolean) {
if (isDbInitialized()) { if (isDbInitialized()) {
throw new Error("DB is already initialized"); throw new Error("DB is already initialized");
} }
const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf-8"); const schema = fs.readFileSync(`${resourceDir.DB_INIT_DIR}/schema.sql`, "utf-8");
const demoFile = customDbBuffer ?? fs.readFileSync(`${resourceDir.DB_INIT_DIR}/demo.zip`); const demoFile = (!skipDemoDb ? fs.readFileSync(`${resourceDir.DB_INIT_DIR}/demo.zip`) : null);
let rootNote!: BNote; let rootNote!: BNote;
@ -118,9 +118,9 @@ async function createInitialDatabase(preserveIds?: boolean, customDbBuffer?: Buf
const dummyTaskContext = new TaskContext("no-progress-reporting", "import", false); const dummyTaskContext = new TaskContext("no-progress-reporting", "import", false);
await zipImportService.importZip(dummyTaskContext, demoFile, rootNote, { if (demoFile) {
preserveIds await zipImportService.importZip(dummyTaskContext, demoFile, rootNote);
}); }
sql.transactional(() => { sql.transactional(() => {
// this needs to happen after ZIP import, // this needs to happen after ZIP import,