mirror of
https://github.com/zadam/trilium.git
synced 2025-11-12 23:01:05 +08:00
refactor(build-docs): use proper await when building documentation
This commit is contained in:
parent
1822eea77c
commit
61d26fec60
1 changed files with 28 additions and 27 deletions
|
|
@ -5,8 +5,8 @@ process.env.NODE_ENV = "development";
|
||||||
import cls from "@triliumnext/server/src/services/cls.js";
|
import cls from "@triliumnext/server/src/services/cls.js";
|
||||||
import { dirname, join, resolve } from "path";
|
import { dirname, join, resolve } from "path";
|
||||||
import fs, { copyFile } from "fs/promises";
|
import fs, { copyFile } from "fs/promises";
|
||||||
import fsExtra, { type WriteStream } from "fs-extra";
|
import fsExtra, { createWriteStream, type WriteStream } from "fs-extra";
|
||||||
import archiver, { type Archiver } from "archiver";
|
import archiver from "archiver";
|
||||||
|
|
||||||
const DOCS_ROOT = "../../../docs";
|
const DOCS_ROOT = "../../../docs";
|
||||||
const OUTPUT_DIR = "../../site";
|
const OUTPUT_DIR = "../../site";
|
||||||
|
|
@ -23,8 +23,16 @@ async function main() {
|
||||||
// Export
|
// Export
|
||||||
const zipFilePath = "output.zip";
|
const zipFilePath = "output.zip";
|
||||||
try {
|
try {
|
||||||
const { exportToZipFile } = (await import("@triliumnext/server/src/services/export/zip.js")).default;
|
const { exportToZip } = (await import("@triliumnext/server/src/services/export/zip.js")).default;
|
||||||
await exportToZipFile(note.noteId, "share", zipFilePath);
|
const branch = note.getParentBranches()[0];
|
||||||
|
const taskContext = new (await import("@triliumnext/server/src/services/task_context.js")).default(
|
||||||
|
"no-progress-reporting",
|
||||||
|
"export",
|
||||||
|
null
|
||||||
|
);
|
||||||
|
const fileOutputStream = createWriteStream(zipFilePath);
|
||||||
|
await exportToZip(taskContext, branch, "share", fileOutputStream);
|
||||||
|
await waitForStreamToFinish(fileOutputStream);
|
||||||
await extractZip(zipFilePath, OUTPUT_DIR);
|
await extractZip(zipFilePath, OUTPUT_DIR);
|
||||||
} finally {
|
} finally {
|
||||||
if (await fsExtra.exists(zipFilePath)) {
|
if (await fsExtra.exists(zipFilePath)) {
|
||||||
|
|
@ -65,7 +73,8 @@ async function createImportZip(path: string) {
|
||||||
|
|
||||||
const outputStream = fsExtra.createWriteStream(inputFile);
|
const outputStream = fsExtra.createWriteStream(inputFile);
|
||||||
archive.pipe(outputStream);
|
archive.pipe(outputStream);
|
||||||
await waitForEnd(archive, outputStream);
|
archive.finalize();
|
||||||
|
await waitForStreamToFinish(outputStream);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await fsExtra.readFile(inputFile);
|
return await fsExtra.readFile(inputFile);
|
||||||
|
|
@ -74,35 +83,27 @@ async function createImportZip(path: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function waitForEnd(archive: Archiver, stream: WriteStream) {
|
function waitForStreamToFinish(stream: WriteStream) {
|
||||||
return new Promise<void>(async (res, rej) => {
|
return new Promise<void>((res, rej) => {
|
||||||
stream.on("finish", () => res());
|
stream.on("finish", () => res());
|
||||||
await archive.finalize();
|
stream.on("error", (err) => rej(err));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function extractZip(zipFilePath: string, outputPath: string, ignoredFiles?: Set<string>) {
|
export async function extractZip(zipFilePath: string, outputPath: string, ignoredFiles?: Set<string>) {
|
||||||
const deferred = (await import("@triliumnext/server/src/services/utils.js")).deferred;
|
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);
|
||||||
|
|
||||||
const promise = deferred<void>()
|
await fsExtra.mkdirs(dirname(destPath));
|
||||||
setTimeout(async () => {
|
await fs.writeFile(destPath, fileContent);
|
||||||
// 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));
|
zip.readEntry();
|
||||||
await fs.writeFile(destPath, fileContent);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
zip.readEntry();
|
|
||||||
});
|
|
||||||
promise.resolve();
|
|
||||||
}, 1000);
|
|
||||||
await promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cls.init(main);
|
cls.init(main);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue