Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tools/gate/skew_ts.mjs

2.7 KiB, 1 run, executable

created by r1870400018:66277, 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// The typst.ts half of `skew.sh`: gathers a project the way Daimond does, compiles it to a PDF with the
2// vendored typst.ts in node, and writes the PDF where `--out` says. It prints closed-vocabulary lines and
3// nothing else: typst.ts's diagnostics quote the source, so they are never printed.
4//
5// node skew_ts.mjs --root <dir> --main <file> --vendor <dir> --out <pdf> [--font-dir <dir>]... [--exclude <dir>]...
6// [--packages <dir>]
7//
8// Prints: `typstts: gathered sources <n> assets <n> fonts <n> packages <n> missing <n>`; one
9// `typstts: package preview <name> <version> <supplied|missing>` for each spec in the registry namespace
10// `preview` (a public name; specs of any other namespace are only counted); then `typstts: compile ok` or
11// `typstts: compile failed`; or `typstts: error <code>` with a code from {bad-args, no-root, no-main,
12// no-packages, bad-package, too-large, failed}.
13
14import fs from 'node:fs';
15import { gather, FONT_DIRS } from '../bench/lib/gather.mjs';
16import { loadTypstTs } from '../bench/lib/wasm_common.mjs';
17
18function args(argv) {
19 const a = { fontDirs: [], exclude: [] };
20 for (let i = 0; i < argv.length; i += 2) {
21 const k = argv[i];
22 const v = argv[i + 1];
23 if (v === undefined) return null;
24 if (k === '--font-dir') a.fontDirs.push(v);
25 else if (k === '--exclude') a.exclude.push(v);
26 else if (k === '--packages') a.packages = v;
27 else if (k === '--root') a.root = v;
28 else if (k === '--main') a.main = v;
29 else if (k === '--vendor') a.vendor = v;
30 else if (k === '--out') a.out = v;
31 else return null;
32 }
33 if (!a.root || !a.main || !a.vendor || !a.out) return null;
34 if (!a.fontDirs.length) a.fontDirs = FONT_DIRS;
35 return a;
36}
37
38const a = args(process.argv.slice(2));
39if (!a) {
40 console.log('typstts: error bad-args');
41 process.exit(2);
42}
43
44let code = 'failed';
45try {
46 const g = gather(a.root, a.main, { fontDirs: a.fontDirs, exclude: a.exclude, packages: a.packages ?? null });
47 console.log(`typstts: gathered sources ${g.stats.sources} assets ${g.stats.assets} fonts ${g.stats.fonts} packages ${g.stats.packages} missing ${g.stats.missing}`);
48 for (const { spec, found } of g.specs) {
49 if (spec.ns === 'preview') console.log(`typstts: package preview ${spec.name} ${spec.version} ${found ? 'supplied' : 'missing'}`);
50 }
51 code = 'compile';
52 const ts = await loadTypstTs(a.vendor, g);
53 const r = ts.compilePdf({ main: g.main, sources: g.sources, assets: g.assets });
54 if (r.pdf && r.pdf.length > 4) {
55 fs.writeFileSync(a.out, r.pdf);
56 console.log('typstts: compile ok');
57 } else {
58 console.log('typstts: compile failed');
59 process.exit(1);
60 }
61} catch (e) {
62 if (code === 'compile') console.log('typstts: compile failed');
63 else console.log(`typstts: error ${e && e.code ? e.code : 'failed'}`);
64 process.exit(1);
65}