mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-30 18:05:55 +01:00 
			
		
		
		
	feat(demo): add entrypoint for editing the demo safely
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -26,6 +26,7 @@ server-package.json | ||||
| .idea/shelf/ | ||||
| data/ | ||||
| data-test/ | ||||
| data-demo/ | ||||
| tmp/ | ||||
| .eslintcache | ||||
|  | ||||
|   | ||||
| @@ -3,81 +3,25 @@ import fsExtra from "fs-extra"; | ||||
| import path from "path"; | ||||
| import type NoteMeta from "./src/services/meta/note_meta.js"; | ||||
| import type { NoteMetaFile } from "./src/services/meta/note_meta.js"; | ||||
| import cls from "./src/services/cls.js"; | ||||
| import { initializeTranslations } from "./src/services/i18n.js"; | ||||
| import archiver, { type Archiver } from "archiver"; | ||||
| import type { WriteStream } from "fs"; | ||||
| import debounce from "./src/public/app/services/debounce.js"; | ||||
| import { importData, initializeDatabase, startElectron } from "./electron-utils.js"; | ||||
|  | ||||
| const NOTE_ID_USER_GUIDE = "pOsGYCXsbNQG"; | ||||
| const markdownPath = path.join("docs", "User Guide"); | ||||
| const htmlPath = path.join("src", "public", "app", "doc_notes", "en", "User Guide"); | ||||
|  | ||||
| async function startElectron() { | ||||
|     await import("./electron-main.js"); | ||||
| } | ||||
|  | ||||
| async function main() { | ||||
|     await initializeTranslations(); | ||||
|     const zipBuffer = await createImportZip(); | ||||
|     await initializeDatabase(); | ||||
|     await importData(zipBuffer); | ||||
|     await importData(zipBuffer, NOTE_ID_USER_GUIDE); | ||||
|     await startElectron(); | ||||
|     await registerHandlers(); | ||||
| } | ||||
|  | ||||
| async function initializeDatabase() { | ||||
|     const sqlInit = (await import("./src/services/sql_init.js")).default; | ||||
|  | ||||
|     cls.init(() => { | ||||
|         if (!sqlInit.isDbInitialized()) { | ||||
|             sqlInit.createInitialDatabase(); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|  | ||||
| function importData(input: Buffer) { | ||||
|     return new Promise<void>((resolve, reject) => { | ||||
|         cls.init(async () => { | ||||
|             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); | ||||
|             const utils = ((await import("./src/services/utils.js")).default); | ||||
|             const eraseService = ((await import("./src/services/erase.js")).default); | ||||
|             const deleteId = utils.randomString(10); | ||||
|  | ||||
|             const existingNote = becca.getNote(NOTE_ID_USER_GUIDE); | ||||
|             if (existingNote) { | ||||
|                 existingNote.deleteNote(deleteId); | ||||
|             } | ||||
|             eraseService.eraseNotesWithDeleteId(deleteId); | ||||
|  | ||||
|             const { note } = 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" | ||||
|             }); | ||||
|  | ||||
|             const TaskContext = (await import("./src/services/task_context.js")).default; | ||||
|             const { importZip } = ((await import("./src/services/import/zip.js")).default); | ||||
|             const context = new TaskContext("no-report"); | ||||
|             await importZip(context, input, note, { preserveIds: true }); | ||||
|  | ||||
|             const { runOnDemandChecks } = (await import("./src/services/consistency_checks.js")).default; | ||||
|             await runOnDemandChecks(true); | ||||
|  | ||||
|             becca.reset(); | ||||
|             beccaLoader.load(); | ||||
|  | ||||
|             resolve(); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
| } | ||||
|  | ||||
| async function createImportZip() { | ||||
|     const inputFile = "input.zip"; | ||||
|     const archive = archiver("zip", { | ||||
|   | ||||
							
								
								
									
										43
									
								
								electron-edit-demo.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								electron-edit-demo.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| import { importData, initializeDatabase, startElectron } from "./electron-utils.js"; | ||||
| import { initializeTranslations } from "./src/services/i18n.js"; | ||||
| import fs from "fs"; | ||||
| import debounce from "./src/public/app/services/debounce.js"; | ||||
|  | ||||
| const DEMO_NOTE_ID = "demo"; | ||||
| const DEMO_ZIP_PATH = "db/demo.zip"; | ||||
|  | ||||
| async function main() { | ||||
|     await initializeTranslations(); | ||||
|     await initializeDatabase(); | ||||
|  | ||||
|     const demoBuffer = fs.readFileSync(DEMO_ZIP_PATH); | ||||
|     await importData(demoBuffer, DEMO_NOTE_ID); | ||||
|  | ||||
|     await startElectron(); | ||||
|     await registerHandlers(); | ||||
| } | ||||
|  | ||||
| async function registerHandlers() { | ||||
|     const events = (await import("./src/services/events.js")).default; | ||||
|     const eraseService = (await import("./src/services/erase.js")).default; | ||||
|     const debouncer = debounce(async () => { | ||||
|         console.log("Exporting data"); | ||||
|         eraseService.eraseUnusedAttachmentsNow(); | ||||
|         await exportData(); | ||||
|     }, 10_000);; | ||||
|     events.subscribe(events.ENTITY_CHANGED, async (e) => { | ||||
|         if (e.entityName === "options") { | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         console.log("Got entity changed ", e); | ||||
|         debouncer(); | ||||
|     }); | ||||
| } | ||||
|  | ||||
| async function exportData() { | ||||
|     const { exportToZipFile } = (await import("./src/services/export/zip.js")).default; | ||||
|     await exportToZipFile(DEMO_NOTE_ID, "html", DEMO_ZIP_PATH); | ||||
| } | ||||
|  | ||||
| await main(); | ||||
							
								
								
									
										57
									
								
								electron-utils.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								electron-utils.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| import cls from "./src/services/cls.js"; | ||||
|  | ||||
| export async function initializeDatabase() { | ||||
|     const sqlInit = (await import("./src/services/sql_init.js")).default; | ||||
|  | ||||
|     cls.init(() => { | ||||
|         if (!sqlInit.isDbInitialized()) { | ||||
|             sqlInit.createInitialDatabase(); | ||||
|         } | ||||
|     }); | ||||
| } | ||||
|  | ||||
| export async function startElectron() { | ||||
|     await import("./electron-main.js"); | ||||
| } | ||||
|  | ||||
| export function importData(input: Buffer, rootId: string) { | ||||
|     return new Promise<void>((resolve, reject) => { | ||||
|         cls.init(async () => { | ||||
|             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); | ||||
|             const utils = ((await import("./src/services/utils.js")).default); | ||||
|             const eraseService = ((await import("./src/services/erase.js")).default); | ||||
|             const deleteId = utils.randomString(10); | ||||
|  | ||||
|             const existingNote = becca.getNote(rootId); | ||||
|             if (existingNote) { | ||||
|                 existingNote.deleteNote(deleteId); | ||||
|             } | ||||
|             eraseService.eraseNotesWithDeleteId(deleteId); | ||||
|  | ||||
|             const { note } = notes.createNewNoteWithTarget("into", "none_root", { | ||||
|                 parentNoteId: "root", | ||||
|                 noteId: rootId, | ||||
|                 title: "User Guide", | ||||
|                 content: "The sub-children of this note are automatically synced.", | ||||
|                 type: "text" | ||||
|             }); | ||||
|  | ||||
|             const TaskContext = (await import("./src/services/task_context.js")).default; | ||||
|             const { importZip } = ((await import("./src/services/import/zip.js")).default); | ||||
|             const context = new TaskContext("no-report"); | ||||
|             await importZip(context, input, note, { preserveIds: true }); | ||||
|  | ||||
|             const { runOnDemandChecks } = (await import("./src/services/consistency_checks.js")).default; | ||||
|             await runOnDemandChecks(true); | ||||
|  | ||||
|             becca.reset(); | ||||
|             beccaLoader.load(); | ||||
|  | ||||
|             resolve(); | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
| } | ||||
| @@ -38,6 +38,7 @@ | ||||
|     "electron:switch": "electron-rebuild", | ||||
|     "docs:edit": "cross-env NODE_OPTIONS=\"--import tsx\" TRILIUM_DATA_DIR=./data-docs TRILIUM_ENV=dev TRILIUM_PORT=37741 electron ./electron-docs-main.ts .", | ||||
|     "docs:edit-nix": "electron-rebuild --version 33.3.1 && cross-env NODE_OPTIONS=\"--import tsx\" TRILIUM_DATA_DIR=./data-docs TRILIUM_PORT=37741 TRILIUM_ENV=dev nix-shell -p electron_33 --run \"electron ./electron-docs-main.ts .\"", | ||||
|     "demo:edit": "cross-env NODE_OPTIONS=\"--import tsx\" TRILIUM_DATA_DIR=./data-demo TRILIUM_ENV=dev TRILIUM_PORT=37741 electron ./electron-edit-demo.ts .", | ||||
|     "electron-forge:start": "npm run build:prepare-dist && cd ./build && electron-forge start", | ||||
|     "electron-forge:make": "npm run build:prepare-dist && cross-env DEBUG=electron-windows-installer:* electron-forge make ./build", | ||||
|     "electron-forge:package": "npm run build:prepare-dist && cd ./build && electron-forge package", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user