mirror of
https://github.com/zadam/trilium.git
synced 2026-02-22 13:44:25 +01:00
feat(pdfjs): add script to update viewer
This commit is contained in:
parent
4955bd782b
commit
a77b5601be
52
packages/pdfjs-viewer/scripts/update-viewer.ts
Normal file
52
packages/pdfjs-viewer/scripts/update-viewer.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { join } from "path";
|
||||
import packageJson from "../package.json" with { type: "json" };
|
||||
import * as yauzl from "yauzl";
|
||||
import { createWriteStream } from "fs";
|
||||
const version = packageJson.devDependencies["pdfjs-dist"];
|
||||
const url = `https://github.com/mozilla/pdf.js/releases/download/v${version}/pdfjs-${version}-dist.zip`;
|
||||
|
||||
const FILES_TO_COPY = [
|
||||
"web/images/",
|
||||
"web/locale/",
|
||||
"web/viewer.css",
|
||||
"web/viewer.html",
|
||||
"web/viewer.mjs"
|
||||
];
|
||||
|
||||
async function main() {
|
||||
console.log(`Downloading pdfjs-dist v${version} from ${url}...`);
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to download pdfjs-dist from ${url}: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const buffer = await response.arrayBuffer();
|
||||
yauzl.fromBuffer(Buffer.from(buffer), { lazyEntries: true }, (err, zip) => {
|
||||
zip.readEntry();
|
||||
zip.on("entry", (entry: yauzl.Entry) => {
|
||||
if (entry.fileName.endsWith("/") || !FILES_TO_COPY.some(prefix => entry.fileName.startsWith(prefix))) {
|
||||
// Directory entry or not in the list of files to copy, skip
|
||||
console.log("Skipping", entry.fileName);
|
||||
zip.readEntry();
|
||||
return;
|
||||
}
|
||||
|
||||
const relativePath = entry.fileName.substring("web/".length);
|
||||
zip.openReadStream(entry, (err, readStream) => {
|
||||
if (err) {
|
||||
console.error(`Failed to read ${entry.fileName} from zip:`, err);
|
||||
return;
|
||||
}
|
||||
const outPath = join(__dirname, "../viewer", relativePath);
|
||||
const outStream = createWriteStream(outPath);
|
||||
readStream.pipe(outStream);
|
||||
outStream.on("finish", () => {
|
||||
console.log(`Extracted ${relativePath} to ${outPath}`);
|
||||
});
|
||||
});
|
||||
zip.readEntry();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
main();
|
||||
Loading…
x
Reference in New Issue
Block a user