mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-28 18:42:28 +08:00
feat(edit-docs): automatically write documentation on start-up
This commit is contained in:
parent
2530c01a31
commit
df2a9aed44
@ -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();
|
||||
|
@ -605,7 +605,7 @@ function streamToBuffer(stream: Stream): Promise<Buffer> {
|
||||
return new Promise((res, rej) => stream.on("end", () => res(Buffer.concat(chunks))));
|
||||
}
|
||||
|
||||
function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise<Buffer> {
|
||||
export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise<Buffer> {
|
||||
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<Buffer
|
||||
});
|
||||
}
|
||||
|
||||
function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => void) {
|
||||
return new Promise((res, rej) => {
|
||||
export function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise<void>) {
|
||||
return new Promise<void>((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.");
|
||||
|
Loading…
x
Reference in New Issue
Block a user