From df2a9aed44d887b492b7597d2b313d8c7ee0f80c Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 10 Mar 2025 16:20:48 +0200 Subject: [PATCH] feat(edit-docs): automatically write documentation on start-up --- electron-docs-main.ts | 36 ++++++++++++++++++++++++++++++++++++ src/services/import/zip.ts | 6 +++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/electron-docs-main.ts b/electron-docs-main.ts index 875748bc3..3c9ab8033 100644 --- a/electron-docs-main.ts +++ b/electron-docs-main.ts @@ -1,9 +1,45 @@ +import fs from "fs/promises"; +import fsExtra from "fs-extra"; +import path from "path"; + +const NOTE_ID_USER_GUIDE = "pOsGYCXsbNQG"; + async function startElectron() { await import("./electron-main.js"); } async function main() { await startElectron(); + await exportData(); +} + +async function exportData() { + const zipFilePath = "output.zip"; + const destRootPath = path.join("src", "public", "app", "doc_notes", "en", "User Guide"); + + await fsExtra.remove(destRootPath); + await fsExtra.mkdir(destRootPath); + + // First export as zip. + const { exportToZipFile } = (await import("./src/services/export/zip.js")).default; + await exportToZipFile(NOTE_ID_USER_GUIDE, "html", zipFilePath); + + setTimeout(async () => { + // Then extract the zip. + const { readZipFile, readContent } = (await import("./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("/")) { + const destPath = path.join(destRootPath, entry.fileName); + const fileContent = await readContent(zip, entry); + + await fsExtra.mkdirs(path.dirname(destPath)); + await fs.writeFile(destPath, fileContent); + } + + zip.readEntry(); + }); + }, 1000); } await main(); diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts index 5251b9546..49cde38f6 100644 --- a/src/services/import/zip.ts +++ b/src/services/import/zip.ts @@ -605,7 +605,7 @@ function streamToBuffer(stream: Stream): Promise { return new Promise((res, rej) => stream.on("end", () => res(Buffer.concat(chunks)))); } -function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise { +export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise { return new Promise((res, rej) => { zipfile.openReadStream(entry, function (err, readStream) { if (err) rej(err); @@ -616,8 +616,8 @@ function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise void) { - return new Promise((res, rej) => { +export function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise) { + return new Promise((res, rej) => { yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false }, function (err, zipfile) { if (err) rej(err); if (!zipfile) throw new Error("Unable to read zip file.");