package file→ .shtukbase/packages/shtukbase-icons/actions/build.ts
actions/build.ts
35 lines, 2.4 KB.
- // shtuk run shtukbase-icons build [--style <id>] [--out <folder>] [--check]
- // Renders every icon of the registry in an icon style (default: the held one, else 79) into the shared set folder of the
- // icons package (/assets/icons/set/<name>.svg), and writes the set as Iconify JSON (/design/shtukbase-icons/iconify.json).
- // --check: writes nothing; exit code 1 when a rendered file or the Iconify JSON is missing or stale.
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
- import { dirname, join } from 'node:path';
- import { iconStyle, settings } from '../../icons/lib/iconlib.ts';
- import { BASE_STYLE, ICONIFY, iconify, iconifyText, outDir, registry, renderIcon, setStyles, styleById } from '../lib/sbicons.ts';
- const root = process.cwd();
- const args = process.argv.slice(2);
- const opt = (k: string) => { const i = args.indexOf(k); return i >= 0 ? args[i + 1] : undefined; };
- const check = args.includes('--check');
- try {
- const held = iconStyle(root, settings(root));
- const mine = setStyles(root);
- const id = opt('--style') ?? (held && mine.some((s) => s.page === held.page) ? held.id : BASE_STYLE);
- const style = styleById(root, id);
- const out = (opt('--out') ?? outDir(root)).replace(/\/+$/, '');
- const reg = registry(root);
- const want = new Map<string, string>();
- for (const rec of reg.icons) want.set(`${out}/${rec.name}.svg`, renderIcon(root, rec, style));
- want.set(ICONIFY, iconifyText(iconify(root, reg, style)));
- const stale = [...want].filter(([p, t]) => { const a = join(root, p.replace(/^\/+/, '')); return !existsSync(a) || readFileSync(a, 'utf8') !== t; }).map(([p]) => p);
- if (check) {
- if (stale.length) { console.log(`${stale.length} stale or missing (style ${style.id}): ${stale.slice(0, 8).join(', ')}${stale.length > 8 ? ', …' : ''} → run: shtuk run shtukbase-icons build --style ${style.id}`); process.exitCode = 1; }
- else console.log(`the set is built in style ${style.id}: ${reg.icons.length} icons in ${out}/, ${ICONIFY}`);
- } else {
- for (const p of stale) { const a = join(root, p.replace(/^\/+/, '')); mkdirSync(dirname(a), { recursive: true }); writeFileSync(a, want.get(p)!); }
- console.log(`style ${style.id} (${style.title}): ${reg.icons.length} icons → ${out}/, Iconify → ${ICONIFY}; ${stale.length} file(s) written`);
- }
- } catch (e) {
- console.error((e as Error).message);
- process.exitCode = 2;
- }