2025-10-28 16:24:55 +02:00
|
|
|
process.env.TRILIUM_INTEGRATION_TEST = "memory-no-store";
|
|
|
|
|
process.env.TRILIUM_RESOURCE_DIR = "../server/src";
|
|
|
|
|
process.env.NODE_ENV = "development";
|
|
|
|
|
|
|
|
|
|
import cls from "@triliumnext/server/src/services/cls.js";
|
2025-10-28 16:42:52 +02:00
|
|
|
import { dirname, join, resolve } from "path";
|
|
|
|
|
import fs from "fs/promises";
|
2025-10-28 16:24:55 +02:00
|
|
|
import fsExtra, { type WriteStream } from "fs-extra";
|
|
|
|
|
import archiver, { type Archiver } from "archiver";
|
|
|
|
|
|
|
|
|
|
const DOCS_ROOT = "../../../docs";
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
const i18n = await import("@triliumnext/server/src/services/i18n.js");
|
|
|
|
|
await i18n.initializeTranslations();
|
|
|
|
|
|
|
|
|
|
const sqlInit = (await import("../../server/src/services/sql_init.js")).default;
|
|
|
|
|
await sqlInit.createInitialDatabase(true);
|
|
|
|
|
|
|
|
|
|
await importData(join(__dirname, DOCS_ROOT, "User Guide"));
|
|
|
|
|
|
|
|
|
|
console.log("DB ready!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function importData(path: string) {
|
|
|
|
|
const buffer = await createImportZip(path);
|
|
|
|
|
const importService = (await import("@triliumnext/server/src/services/import/zip.js")).default;
|
|
|
|
|
const TaskContext = (await import("@triliumnext/server/src/services/task_context.js")).default;
|
|
|
|
|
const context = new TaskContext("no-progress-reporting", "importNotes", null);
|
|
|
|
|
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.");
|
|
|
|
|
}
|
|
|
|
|
const note = await importService.importZip(context, buffer, rootNote, {
|
|
|
|
|
preserveIds: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Export
|
|
|
|
|
const zipFilePath = "output.zip";
|
2025-10-28 16:42:52 +02:00
|
|
|
try {
|
|
|
|
|
const { exportToZipFile } = (await import("@triliumnext/server/src/services/export/zip.js")).default;
|
|
|
|
|
await exportToZipFile(note.noteId, "share", zipFilePath);
|
|
|
|
|
await extractZip(zipFilePath, "../../site");
|
|
|
|
|
} finally {
|
|
|
|
|
if (await fsExtra.exists(zipFilePath)) {
|
|
|
|
|
await fsExtra.rm(zipFilePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-28 16:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-28 16:42:52 +02:00
|
|
|
export async function extractZip(zipFilePath: string, outputPath: string, ignoredFiles?: Set<string>) {
|
|
|
|
|
const deferred = (await import("@triliumnext/server/src/services/utils.js")).deferred;
|
|
|
|
|
|
|
|
|
|
const promise = deferred<void>()
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
// Then extract the zip.
|
|
|
|
|
const { readZipFile, readContent } = (await import("@triliumnext/server/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("/") && !ignoredFiles?.has(entry.fileName)) {
|
|
|
|
|
const destPath = join(outputPath, entry.fileName);
|
|
|
|
|
const fileContent = await readContent(zip, entry);
|
|
|
|
|
|
|
|
|
|
await fsExtra.mkdirs(dirname(destPath));
|
|
|
|
|
await fs.writeFile(destPath, fileContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zip.readEntry();
|
|
|
|
|
});
|
|
|
|
|
promise.resolve();
|
|
|
|
|
}, 1000);
|
|
|
|
|
await promise;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-28 16:24:55 +02:00
|
|
|
cls.init(main);
|