oxedyne/fe2o3/fe2o3_austenite/tools/gate/door_pdf.mjs
4.5 KiB, 1 run, executable
created by r1870400018:66257, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env node |
| 2 | // The door half of the gate: gathers a project as Daimond does, supplies its packages, and compiles it to a PDF |
| 3 | // through the wasm package's `compileProject` with `strict: true`, as the browser does. It prints a closed |
| 4 | // vocabulary and nothing else: a diagnostic's message can quote the source, so none is ever printed. |
| 5 | // |
| 6 | // node door_pdf.mjs <pkg> <root> <main> <fontdir> <out> [--packages <dir>] [--exclude <dir>]... [--again <out2>] |
| 7 | // |
| 8 | // <pkg> the wasm-pack output directory (`oxedyne_fe2o3_austenite.js` and its `_bg.wasm`) |
| 9 | // <root> the project root |
| 10 | // <main> the entry file, relative to <root> |
| 11 | // <fontdir> the project's font directory, relative to <root> or absolute |
| 12 | // <out> where the PDF is written, when the compile succeeds |
| 13 | // --packages a directory of packages, as `tools/bench/lib/packages.mjs` describes (Typst's own layout or |
| 14 | // Daimond's `.pack` files); each package the project's imports reach is handed over with |
| 15 | // `supplyPackage`, with its dependencies |
| 16 | // --exclude a directory under <root> the gather leaves out; repeatable |
| 17 | // --again compile a second time on the same instance and write that PDF to <out2>, for G12 |
| 18 | // |
| 19 | // Prints one of |
| 20 | // |
| 21 | // door <ok|error> pages <n|none> kinds <none|kind:n,kind:n,...> needs <n> |
| 22 | // door again <ok|error> (only with --again) |
| 23 | // door fail <bad-args|bad-pkg|no-root|no-main|no-packages|bad-package|too-large|supply|failed> |
| 24 | // |
| 25 | // `kinds` counts the diagnostics of the first compile by their `kind`, from the engine's closed list (a word |
| 26 | // outside it prints as `other`); `needs` is how many packages an import asked for that were not supplied. |
| 27 | |
| 28 | import fs from 'node:fs'; |
| 29 | import path from 'node:path'; |
| 30 | |
| 31 | import { gather } from '../bench/lib/gather.mjs'; |
| 32 | import { specText } from '../bench/lib/packages.mjs'; |
| 33 | import { loadAustenite } from '../bench/lib/wasm_common.mjs'; |
| 34 | |
| 35 | const KINDS = ['missing_file', 'encoding', 'missing_font', 'syntax', 'type', 'unknown_variable', 'package', 'limit', 'unsupported', 'lint', 'internal']; |
| 36 | |
| 37 | function args(argv) { |
| 38 | const pos = []; |
| 39 | const a = { exclude: [], packages: null, again: null }; |
| 40 | for (let i = 0; i < argv.length; i++) { |
| 41 | const k = argv[i]; |
| 42 | if (k === '--packages' || k === '--exclude' || k === '--again') { |
| 43 | if (i + 1 >= argv.length) return null; |
| 44 | const v = argv[++i]; |
| 45 | if (k === '--exclude') a.exclude.push(v); |
| 46 | else if (k === '--packages') a.packages = v; |
| 47 | else a.again = v; |
| 48 | } else if (k.startsWith('--')) { |
| 49 | return null; |
| 50 | } else { |
| 51 | pos.push(k); |
| 52 | } |
| 53 | } |
| 54 | if (pos.length !== 5) return null; |
| 55 | [a.pkg, a.root, a.main, a.fontdir, a.out] = pos; |
| 56 | return a; |
| 57 | } |
| 58 | |
| 59 | function kinds(r) { |
| 60 | const n = {}; |
| 61 | for (const d of (r && r.diagnostics) || []) { |
| 62 | const k = KINDS.includes(d && d.kind) ? d.kind : 'other'; |
| 63 | n[k] = (n[k] || 0) + 1; |
| 64 | } |
| 65 | const keys = Object.keys(n).sort(); |
| 66 | return keys.length ? keys.map((k) => `${k}:${n[k]}`).join(',') : 'none'; |
| 67 | } |
| 68 | |
| 69 | const a = args(process.argv.slice(2)); |
| 70 | if (!a) { |
| 71 | console.log('door fail bad-args'); |
| 72 | process.exit(2); |
| 73 | } |
| 74 | |
| 75 | let stage = 'failed'; |
| 76 | try { |
| 77 | if (!fs.existsSync(path.join(a.pkg, 'oxedyne_fe2o3_austenite.js'))) { |
| 78 | stage = 'bad-pkg'; |
| 79 | throw new Error('no package'); |
| 80 | } |
| 81 | const g = gather(a.root, a.main, { fontDirs: [a.fontdir], exclude: a.exclude, packages: a.packages }); |
| 82 | const aus = await loadAustenite(a.pkg); |
| 83 | const door = aus.instance; |
| 84 | stage = 'supply'; |
| 85 | for (const { spec, pack } of g.packages) { |
| 86 | const r = door.supplyPackage(specText(spec), pack.files); |
| 87 | if (!r || r.error) throw new Error('supply'); |
| 88 | } |
| 89 | stage = 'failed'; |
| 90 | const project = { main: g.main, sources: g.sources, assets: g.assets, fonts: g.fonts, strict: true }; |
| 91 | const once = (to) => { |
| 92 | const r = door.compileProject(project); |
| 93 | const ok = !!(r && r.pdf && r.pdf.length > 4); |
| 94 | if (ok) fs.writeFileSync(to, r.pdf); |
| 95 | return { r, ok }; |
| 96 | }; |
| 97 | const first = once(a.out); |
| 98 | const pages = first.r && Number.isInteger(first.r.pages) ? first.r.pages : 'none'; |
| 99 | const needs = first.r && Array.isArray(first.r.needs) ? first.r.needs.length : 0; |
| 100 | console.log(`door ${first.ok ? 'ok' : 'error'} pages ${pages} kinds ${kinds(first.r)} needs ${needs}`); |
| 101 | if (a.again) { |
| 102 | console.log(`door again ${once(a.again).ok ? 'ok' : 'error'}`); |
| 103 | } |
| 104 | process.exit(first.ok ? 0 : 1); |
| 105 | } catch (e) { |
| 106 | // Only a closed code: a message could name a file of the project. |
| 107 | const code = e && e.code ? e.code : stage; |
| 108 | console.log(`door fail ${['bad-args', 'bad-pkg', 'no-root', 'no-main', 'no-packages', 'bad-package', 'too-large', 'supply', 'failed'].includes(code) ? code : 'failed'}`); |
| 109 | process.exit(2); |
| 110 | } |