GdzieWjade/scripts/make-icons.mjs
2026-10-03 13:33:50 +02:00

23 lines
1.2 KiB
JavaScript

// Generates PWA icons from the USWDS `accessible_forward` glyph (public domain / MIT).
import { readFile, writeFile } from 'node:fs/promises';
import sharp from 'sharp';
const sprite = await readFile('public/uswds/sprite.svg', 'utf8');
const inner = sprite.match(/<symbol[^>]*id="accessible_forward"[^>]*>([\s\S]*?)<\/symbol>/)?.[1];
if (!inner) throw new Error('glyph not found');
const svg = (size, scale, rounded) => `<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 ${size} ${size}">
<rect width="${size}" height="${size}" rx="${rounded ? size * 0.18 : 0}" fill="#005ea2"/>
<g fill="#ffffff" transform="translate(${(size - 24 * scale) / 2} ${(size - 24 * scale) / 2}) scale(${scale})">${inner}</g>
</svg>`;
await writeFile('public/icons/icon.svg', svg(512, 512 * 0.62 / 24, true));
for (const [name, size, scale, rounded] of [
['icon-192.png', 192, 192 * 0.62 / 24, true],
['icon-512.png', 512, 512 * 0.62 / 24, true],
['icon-maskable-512.png', 512, 512 * 0.5 / 24, false],
['apple-touch-icon.png', 180, 180 * 0.62 / 24, false],
]) {
await sharp(Buffer.from(svg(size, scale, rounded))).png().toFile(`public/icons/${name}`);
}
console.log('icons ok');