mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	chore(edit-docs): change import mechanism
This commit is contained in:
		| @@ -10,6 +10,7 @@ import debounce from "./src/public/app/services/debounce.js"; | ||||
| import { extractZip, initializeDatabase, startElectron } from "./electron-utils.js"; | ||||
| import cls from "./src/services/cls.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 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() { | ||||
|     await initializeTranslations(); | ||||
|     const zipBuffer = await createImportZip(); | ||||
|     await initializeDatabase(zipBuffer); | ||||
|     await initializeDatabase(true); | ||||
|  | ||||
|     cls.init(async () => { | ||||
|         await importData(markdownPath); | ||||
|         setOptions(); | ||||
|     }); | ||||
|  | ||||
|     await startElectron(); | ||||
|     cls.init(() => setOptions()); | ||||
|  | ||||
|     // Wait for the import to be finished and the application to be loaded before we listen to changes. | ||||
|     setTimeout(() => registerHandlers(), 10_000); | ||||
| @@ -34,13 +38,28 @@ async function setOptions() { | ||||
|     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 archive = archiver("zip", { | ||||
|         zlib: { level: 0 } | ||||
|     }); | ||||
|  | ||||
|     archive.directory(markdownPath, "/"); | ||||
|     archive.directory(path, "/"); | ||||
|  | ||||
|     const outputStream = fsExtra.createWriteStream(inputFile); | ||||
|     archive.pipe(outputStream); | ||||
|   | ||||
| @@ -3,12 +3,12 @@ import fs from "fs/promises"; | ||||
| import fsExtra from "fs-extra"; | ||||
| import path from "path"; | ||||
|  | ||||
| export function initializeDatabase(customDbBuffer?: Buffer) { | ||||
| export function initializeDatabase(skipDemoDb: boolean) { | ||||
|     return new Promise<void>(async (resolve) => { | ||||
|         const sqlInit = (await import("./src/services/sql_init.js")).default; | ||||
|         cls.init(async () => { | ||||
|             if (!sqlInit.isDbInitialized()) { | ||||
|                 await sqlInit.createInitialDatabase(true, customDbBuffer); | ||||
|                 await sqlInit.createInitialDatabase(skipDemoDb); | ||||
|             } | ||||
|             resolve(); | ||||
|         }); | ||||
|   | ||||
| @@ -67,16 +67,16 @@ async function initDbConnection() { | ||||
| /** | ||||
|  * 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 customDbBuffer a custom database buffer to use, otherwise the default demo one is going to be used. | ||||
|  * @param skipDemoDb if set to `true`, then the demo database will not be imported, resulting in an empty root note. | ||||
|  * @throws {Error} if the database is already initialized. | ||||
|  */ | ||||
| async function createInitialDatabase(preserveIds?: boolean, customDbBuffer?: Buffer) { | ||||
| async function createInitialDatabase(skipDemoDb?: boolean) { | ||||
|     if (isDbInitialized()) { | ||||
|         throw new Error("DB is already initialized"); | ||||
|     } | ||||
|  | ||||
|     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; | ||||
|  | ||||
| @@ -118,9 +118,9 @@ async function createInitialDatabase(preserveIds?: boolean, customDbBuffer?: Buf | ||||
|  | ||||
|     const dummyTaskContext = new TaskContext("no-progress-reporting", "import", false); | ||||
|  | ||||
|     await zipImportService.importZip(dummyTaskContext, demoFile, rootNote, { | ||||
|         preserveIds | ||||
|     }); | ||||
|     if (demoFile) { | ||||
|         await zipImportService.importZip(dummyTaskContext, demoFile, rootNote); | ||||
|     } | ||||
|  | ||||
|     sql.transactional(() => { | ||||
|         // this needs to happen after ZIP import, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user