mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
feat(edit-docs): read from input directory instead of the zip
This commit is contained in:
parent
0bb294753f
commit
ad0c73d210
@ -1,12 +1,13 @@
|
||||
import { extractZip, initializeDatabase, startElectron } from "./utils.js";
|
||||
import { extractZip, importData, initializeDatabase, startElectron } from "./utils.js";
|
||||
import { initializeTranslations } from "@triliumnext/server/src/services/i18n.js";
|
||||
import debounce from "@triliumnext/client/src/services/debounce.js";
|
||||
import fs from "fs/promises";
|
||||
import { join } from "path";
|
||||
import cls from "@triliumnext/server/src/services/cls.js";
|
||||
|
||||
// Paths are relative to apps/edit-docs/dist.
|
||||
const DEMO_ZIP_PATH = join(__dirname, "../../server/src/assets/db/demo.zip");
|
||||
const OUTPUT_DIR = join(__dirname, "../demo");
|
||||
const DEMO_ZIP_DIR_PATH = join(__dirname, "../demo");
|
||||
|
||||
async function main() {
|
||||
const initializedPromise = startElectron(() => {
|
||||
@ -15,10 +16,23 @@ async function main() {
|
||||
});
|
||||
|
||||
await initializeTranslations();
|
||||
await initializeDatabase(false);
|
||||
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() {
|
||||
const events = (await import("@triliumnext/server/src/services/events.js")).default;
|
||||
const eraseService = (await import("@triliumnext/server/src/services/erase.js")).default;
|
||||
@ -27,8 +41,8 @@ async function registerHandlers() {
|
||||
eraseService.eraseUnusedAttachmentsNow();
|
||||
await exportData();
|
||||
|
||||
await fs.rmdir(OUTPUT_DIR, { recursive: true }).catch(() => {});
|
||||
await extractZip(DEMO_ZIP_PATH, OUTPUT_DIR);
|
||||
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") {
|
||||
|
@ -3,15 +3,11 @@ import fsExtra from "fs-extra";
|
||||
import path from "path";
|
||||
import type { NoteMetaFile } from "@triliumnext/server/src/services/meta/note_meta.js";
|
||||
import { initializeTranslations } from "@triliumnext/server/src/services/i18n.js";
|
||||
import archiver, { type Archiver } from "archiver";
|
||||
import type { WriteStream } from "fs";
|
||||
import debounce from "@triliumnext/client/src/services/debounce.js";
|
||||
import { extractZip, initializeDatabase, startElectron } from "./utils.js";
|
||||
import { extractZip, importData, initializeDatabase, startElectron } from "./utils.js";
|
||||
import cls from "@triliumnext/server/src/services/cls.js";
|
||||
import type { AdvancedExportOptions } from "@triliumnext/server/src/services/export/zip.js";
|
||||
import TaskContext from "@triliumnext/server/src/services/task_context.js";
|
||||
import { parseNoteMetaFile } from "@triliumnext/server/src/services/in_app_help.js";
|
||||
import { resolve } from "path";
|
||||
import type NoteMeta from "@triliumnext/server/src/services/meta/note_meta.js";
|
||||
|
||||
interface NoteMapping {
|
||||
@ -79,49 +75,6 @@ async function setOptions() {
|
||||
optionsService.setOption("compressImages", "false");
|
||||
}
|
||||
|
||||
async function importData(path: string) {
|
||||
const buffer = await createImportZip(path);
|
||||
const importService = (await import("@triliumnext/server/src/services/import/zip.js")).default;
|
||||
const context = new TaskContext("no-progress-reporting", "import", false);
|
||||
const becca = (await import("@triliumnext/server/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 }
|
||||
});
|
||||
|
||||
console.log("Archive path is ", resolve(path))
|
||||
archive.directory(path, "/");
|
||||
|
||||
const outputStream = fsExtra.createWriteStream(inputFile);
|
||||
archive.pipe(outputStream);
|
||||
await waitForEnd(archive, outputStream);
|
||||
|
||||
try {
|
||||
return await fsExtra.readFile(inputFile);
|
||||
} finally {
|
||||
await fsExtra.rm(inputFile);
|
||||
}
|
||||
}
|
||||
|
||||
function waitForEnd(archive: Archiver, stream: WriteStream) {
|
||||
return new Promise<void>(async (res, rej) => {
|
||||
stream.on("finish", () => res());
|
||||
await archive.finalize();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
async function exportData(noteId: string, format: "html" | "markdown", outputPath: string, ignoredFiles?: Set<string>) {
|
||||
const zipFilePath = "output.zip";
|
||||
|
||||
|
@ -5,6 +5,10 @@ import path from "path";
|
||||
import electron from "electron";
|
||||
import { deferred, type DeferredPromise } from "@triliumnext/server/src/services/utils.js";
|
||||
import windowService from "@triliumnext/server/src/services/window.js";
|
||||
import archiver, { type Archiver } from "archiver";
|
||||
import type { WriteStream } from "fs";
|
||||
import TaskContext from "@triliumnext/server/src/services/task_context.js";
|
||||
import { resolve } from "path";
|
||||
|
||||
export function initializeDatabase(skipDemoDb: boolean) {
|
||||
return new Promise<void>(async (resolve) => {
|
||||
@ -44,6 +48,48 @@ export function startElectron(callback: () => void): DeferredPromise<void> {
|
||||
return initializedPromise;
|
||||
}
|
||||
|
||||
export async function importData(path: string) {
|
||||
const buffer = await createImportZip(path);
|
||||
const importService = (await import("@triliumnext/server/src/services/import/zip.js")).default;
|
||||
const context = new TaskContext("no-progress-reporting", "import", false);
|
||||
const becca = (await import("@triliumnext/server/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 }
|
||||
});
|
||||
|
||||
console.log("Archive path is ", resolve(path))
|
||||
archive.directory(path, "/");
|
||||
|
||||
const outputStream = fsExtra.createWriteStream(inputFile);
|
||||
archive.pipe(outputStream);
|
||||
await waitForEnd(archive, outputStream);
|
||||
|
||||
try {
|
||||
return await fsExtra.readFile(inputFile);
|
||||
} finally {
|
||||
await fsExtra.rm(inputFile);
|
||||
}
|
||||
}
|
||||
|
||||
function waitForEnd(archive: Archiver, stream: WriteStream) {
|
||||
return new Promise<void>(async (res, rej) => {
|
||||
stream.on("finish", () => res());
|
||||
await archive.finalize();
|
||||
});
|
||||
}
|
||||
|
||||
export async function extractZip(zipFilePath: string, outputPath: string, ignoredFiles?: Set<string>) {
|
||||
const deferred = (await import("@triliumnext/server/src/services/utils.js")).deferred;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user