Something went wrong. Try again.
Web frontend and supporting services for lance.blue
Something went wrong. Try again.
2.2 kB · 58 lines
JavaScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859/** * A published unit index, for the tests that check the catalog resolves in it. * * Not committed: it is 3.7MB of MegaMek-derived data and it belongs to the * assets bucket, so a checkout does not have one. Point `UNITS_INDEX` at a * file `helm index` wrote, or at one pulled from the bucket: * * curl -o /tmp/units.json \ * https://lance.blue/assets/helm/<helm release>/<megamek>/units.json * UNITS_INDEX=/tmp/units.json npm test * * Absent, the tests that need it skip and say so. They guard a join between * two things this repository does not own — the scenario files it vendors and * the library helm publishes — so a skip is honest: there is nothing local to * check them against. */import { readFile } from "node:fs/promises";
/** * Design name to sprite path, or null when no index was named. * * helm publishes the library split: `units.json` is the spine and the * pictures are `art.json` beside it, joined by position. An older prefix * carries the column on the spine, so both are read - the spine having no * sprite on any design is what tells the two apart. */export async function unitIndex() { const path = process.env.UNITS_INDEX; if (!path) return null; const doc = JSON.parse(await readFile(path, "utf8")); const units = doc.units ?? [];
const sprites = new Map(); if (units.some((unit) => unit.sprite)) { for (const unit of units) { if (unit.sprite) sprites.set(unit.name, unit.sprite); } return sprites; }
const art = JSON.parse( await readFile(path.replace(/units\.json$/, "art.json"), "utf8"), ); // A document of another build joined by position would give designs each // other's pictures, which is worse than the skip an absent index takes. if (art.chunk !== "art" || art.rows?.length !== units.length) throw new Error(`${path}: art.json does not belong to this index`); if (doc.build && art.build?.id !== doc.build.id) throw new Error(`${path}: art.json is from another build`);
units.forEach((unit, at) => { if (art.rows[at]) sprites.set(unit.name, art.rows[at]); }); return sprites;}
export const SKIP = "set UNITS_INDEX to a units.json from `helm index` or the assets bucket";