|
1 | | -import { readFileSync, writeFileSync } from "node:fs"; |
2 | | -import { join } from "node:path"; |
| 1 | +import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; |
| 2 | +import { join, resolve } from "node:path"; |
3 | 3 |
|
4 | | -const scripts = { |
5 | | - "load-a-pdf.js": "Load a PDF", |
6 | | - "create-a-pdf.js": "Create a PDF", |
7 | | - "add-an-annotation.js": "Add an annotation", |
8 | | - "convert-pdf-to-plain-text.js": "Convert PDF to plain text", |
9 | | - "convert-pdf-to-images.js": "Convert PDF to images", |
10 | | - "create-pdf.js": "Create PDF", |
11 | | - "create-pdf-low-level.js": "Create PDF (low level)", |
12 | | - "merge-pdfs.js": "Merge PDFs", |
13 | | - "create-pdf-with-thumbnails.js": "Create PDF with thumbnails", |
14 | | - "draw-graphics-with-device.js": "Draw graphics with Device", |
15 | | - "trace-device.js": "Trace device", |
16 | | - "pdf-dejpx.js": "Replace JPEG 2000 images", |
17 | | - "copy-outline.js": "Copy outline", |
18 | | - "pdf-bake.js": "Bake annotations", |
19 | | -}; |
| 4 | +const root = resolve(import.meta.dir, ".."); |
| 5 | +const kDefaultScript = "load-a-pdf.js"; |
| 6 | +const kScriptSeparator = "// ---"; |
20 | 7 |
|
21 | | -const root = join(import.meta.dir, ".."); |
22 | | -let out = "// Generated by cmd/gen-pdf-scripts.ts.\nstatic const PDFScriptExample kPDFScripts[] = {\n"; |
23 | | -for (const [file, title] of Object.entries(scripts)) { |
24 | | - const source = readFileSync(join(root, "src/pdf-scripts", file), "utf8").replaceAll("\r\n", "\n"); |
25 | | - if (source.includes(')JS"')) throw new Error(`Raw-string delimiter in ${file}`); |
26 | | - out += ` {StrL(${JSON.stringify(title)}), StrL(R"JS(${source})JS")},\n`; |
| 8 | +export function genPDFScripts(inputRoot = root): string { |
| 9 | + const scriptsDir = join(inputRoot, "docs/scripts"); |
| 10 | + const names = readdirSync(scriptsDir, { withFileTypes: true }) |
| 11 | + .filter((file) => file.isFile() && file.name.endsWith(".js")) |
| 12 | + .map((file) => file.name) |
| 13 | + .sort(); |
| 14 | + const first = names.indexOf(kDefaultScript); |
| 15 | + if (first >= 0) names.unshift(...names.splice(first, 1)); |
| 16 | + const scripts = names.map((name) => { |
| 17 | + const source = readFileSync(join(scriptsDir, name), "utf8").replaceAll("\r\n", "\n").trimEnd(); |
| 18 | + if (source.includes(kScriptSeparator)) throw new Error(`${name}: contains the script separator`); |
| 19 | + if (!source.trim()) throw new Error(`${name}: empty script`); |
| 20 | + return source; |
| 21 | + }); |
| 22 | + if (!scripts.length) throw new Error("No PDF scripts found"); |
| 23 | + const bundle = scripts.join(`\n${kScriptSeparator}\n`) + "\n"; |
| 24 | + const output = join(inputRoot, ".work/scripts.js"); |
| 25 | + mkdirSync(join(inputRoot, ".work"), { recursive: true }); |
| 26 | + if (!existsSync(output) || readFileSync(output, "utf8") !== bundle) writeFileSync(output, bundle); |
| 27 | + return output; |
| 28 | +} |
| 29 | + |
| 30 | +if (import.meta.main) { |
| 31 | + console.log(genPDFScripts()); |
27 | 32 | } |
28 | | -out += "};\n"; |
29 | | -writeFileSync(join(root, "src/shared/PDFScriptExamples.inc"), out); |
|
0 commit comments