файл пакета→ .shtukbase/packages/icons/checks/i01_valid.ts
checks/i01_valid.ts
58 строк, 2.6 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // ICN-R01: an icon a page refers to exists and is a valid, safe SVG by the contract (and by the installed icon style).
- // Checked: `icon` and `art` of every page, `icons.item` of every collection, and every .svg file in the icons folder.
- import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- import { Icons, finish, iconStyle, isGlyph, isSvgPath, norm, ownIcons } from '../lib/iconlib.ts';
- import { validate } from '../lib/svg.ts';
- const ic = new Icons();
- const n = norm(ic.root, 'ICN-R01');
- const style = iconStyle(ic.root, ic.s);
- const out: string[] = [];
- const checked = new Map<string, string[]>(); // file -> errors
- function file(path: string): string[] {
- if (checked.has(path)) return checked.get(path)!;
- const abs = join(ic.root, path.slice(1));
- let errs: string[];
- if (!existsSync(abs) || !statSync(abs).isFile()) errs = ['the file does not exist'];
- else errs = validate(readFileSync(abs, 'utf8'), ic.s.maxBytes, style?.rules).filter((f) => f.level === 'error').map((f) => f.text);
- checked.set(path, errs);
- return errs;
- }
- const fix = (path: string) => `run: shtuk run icons sanitize ${path.slice(1)} --write, then validate; a wrong viewBox or size needs a redraw`;
- function ref(where: string, field: string, v: string, glyphOk: boolean) {
- if (!v) return;
- if (glyphOk && isGlyph(v)) return;
- if (!isSvgPath(v)) {
- out.push(`${where}: ${field} "${v}" is ${glyphOk ? 'neither an emoji nor a path to an .svg file' : 'not a path to an .svg file'} → ${glyphOk ? 'write one emoji or /assets/icons/<name>.svg' : `write ${ic.s.folder}/<name>.svg`}`);
- return;
- }
- const path = ic.resolve(where, v);
- if (!ic.inFolder(path)) out.push(`${where}: ${field} ${path} lies outside ${ic.s.folder}/ → move the file there and fix the path`);
- const errs = file(path);
- if (errs.length) out.push(`${where}: ${field} ${path}: ${errs.join('; ')} → ${fix(path)}`);
- }
- if (n.active) {
- for (const p of ic.pages()) {
- const own = ownIcons(p);
- ref(p.id, 'icon', own.icon, true);
- ref(p.id, 'art', own.art, false);
- }
- for (const c of Object.values(ic.collections())) ref(c.coll + '.md', 'icons.item', c.item, true);
- const walk = (rel: string) => {
- const abs = join(ic.root, rel.slice(1));
- if (!existsSync(abs)) return;
- for (const name of readdirSync(abs).sort()) {
- const r = rel + '/' + name;
- if (statSync(join(abs, name)).isDirectory()) walk(r);
- else if (name.toLowerCase().endsWith('.svg') && !checked.has(r)) {
- const errs = file(r);
- if (errs.length) out.push(`${r}: ${errs.join('; ')} → ${fix(r)}`);
- }
- }
- };
- walk(ic.s.folder);
- }
- finish(n, out);