54 lines
2.9 KiB
JavaScript
54 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
// Copies the npm-managed frontend libraries from node_modules/ into
|
|
// public/vendor/ (committed, served as-is, no bundler). package.json pins
|
|
// the versions; Renovate bumps them and runs this as a postUpgradeTask so
|
|
// its PR carries the new files too. Manually: `npm ci && node bin/vendor-sync.mjs`.
|
|
// Assets not on npm: fonts and the Liberty style/sprite go through
|
|
// bin/assets-sync.mjs, the NPS plugins are updated by hand.
|
|
import { copyFileSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
|
|
const root = new URL("..", import.meta.url).pathname;
|
|
const nm = (p) => `${root}node_modules/${p}`;
|
|
const vendor = (p) => `${root}public/vendor/${p}`;
|
|
const copy = (from, to) => copyFileSync(nm(from), vendor(to));
|
|
|
|
for (const f of ["maplibre-gl.css", "maplibre-gl.mjs", "maplibre-gl-shared.mjs", "maplibre-gl-worker.mjs"])
|
|
copy(`maplibre-gl/dist/${f}`, `maplibre-gl/${f}`);
|
|
|
|
copy("pmtiles/dist/pmtiles.js", "pmtiles/pmtiles.js");
|
|
copy("maplibre-contour/dist/index.min.js", "maplibre-contour/maplibre-contour.min.js");
|
|
|
|
// Trailing sourceMappingURL dropped: the .map isn't vendored.
|
|
writeFileSync(
|
|
vendor("maplibre-gl-lanes/maplibre-gl-lanes.mjs"),
|
|
readFileSync(nm("maplibre-gl-lanes/dist/maplibre-gl-lanes.mjs"), "utf8").replace(/\n\/\/# sourceMappingURL=.*\n?$/, "\n"),
|
|
);
|
|
copy("maplibre-gl-lanes/LICENSE", "maplibre-gl-lanes/LICENSE");
|
|
|
|
copy("suncalc/index.js", "suncalc/suncalc.js");
|
|
copy("suncalc/LICENSE", "suncalc/LICENSE");
|
|
|
|
// Only the flags of the countries the offline region picker lists
|
|
// (lib/maptool/data/geofabrik_regions.geojson, from bin/geofabrik-regions-sync.mjs).
|
|
const regionCatalog = JSON.parse(readFileSync(`${root}lib/maptool/data/geofabrik_regions.geojson`, "utf8"));
|
|
const flagCodes = new Set(regionCatalog.features.map((f) => f.properties.code.match(/^([A-Z]{2})(-|$)/)?.[1]).filter(Boolean));
|
|
for (const code of flagCodes) copy(`flag-icons/flags/1x1/${code.toLowerCase()}.svg`, `flag-icons/1x1/${code.toLowerCase()}.svg`);
|
|
|
|
// Material Design Icons: only the mdi* names public/js/ actually uses (the
|
|
// whole set is ~3 MB), as one small ES module. Using another icon is just
|
|
// naming it in the code and running this again.
|
|
const mdi = await import(nm("@mdi/js/mdi.js"));
|
|
const used = new Set();
|
|
for (const f of readdirSync(`${root}public/js`)) {
|
|
if (f.endsWith(".js")) for (const [name] of readFileSync(`${root}public/js/${f}`, "utf8").matchAll(/\bmdi[A-Z]\w*/g)) used.add(name);
|
|
}
|
|
const missing = [...used].filter((name) => !mdi[name]);
|
|
if (missing.length) throw new Error(`not in @mdi/js: ${missing.join(", ")}`);
|
|
mkdirSync(vendor("mdi"), { recursive: true });
|
|
writeFileSync(
|
|
vendor("mdi/mdi.mjs"),
|
|
`// Generated by bin/vendor-sync.mjs from @mdi/js ${JSON.parse(readFileSync(nm("@mdi/js/package.json"), "utf8")).version}, Apache-2.0 (LICENSE).\n` +
|
|
[...used].sort().map((name) => `export const ${name} = "${mdi[name]}";\n`).join(""),
|
|
);
|
|
copy("@mdi/js/LICENSE", "mdi/LICENSE");
|