ShtukbaseStandard
package file→ .shtukbase/packages/icons/checks/i01_valid.ts

checks/i01_valid.ts

58 lines, 2.6 KB.

  1. // ICN-R01: an icon a page refers to exists and is a valid, safe SVG by the contract (and by the installed icon style).
  2. // Checked: `icon` and `art` of every page, `icons.item` of every collection, and every .svg file in the icons folder.
  3. import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { Icons, finish, iconStyle, isGlyph, isSvgPath, norm, ownIcons } from '../lib/iconlib.ts';
  6. import { validate } from '../lib/svg.ts';
  7. const ic = new Icons();
  8. const n = norm(ic.root, 'ICN-R01');
  9. const style = iconStyle(ic.root, ic.s);
  10. const out: string[] = [];
  11. const checked = new Map<string, string[]>(); // file -> errors
  12. function file(path: string): string[] {
  13. if (checked.has(path)) return checked.get(path)!;
  14. const abs = join(ic.root, path.slice(1));
  15. let errs: string[];
  16. if (!existsSync(abs) || !statSync(abs).isFile()) errs = ['the file does not exist'];
  17. else errs = validate(readFileSync(abs, 'utf8'), ic.s.maxBytes, style?.rules).filter((f) => f.level === 'error').map((f) => f.text);
  18. checked.set(path, errs);
  19. return errs;
  20. }
  21. const fix = (path: string) => `run: shtuk run icons sanitize ${path.slice(1)} --write, then validate; a wrong viewBox or size needs a redraw`;
  22. function ref(where: string, field: string, v: string, glyphOk: boolean) {
  23. if (!v) return;
  24. if (glyphOk && isGlyph(v)) return;
  25. if (!isSvgPath(v)) {
  26. 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`}`);
  27. return;
  28. }
  29. const path = ic.resolve(where, v);
  30. if (!ic.inFolder(path)) out.push(`${where}: ${field} ${path} lies outside ${ic.s.folder}/ → move the file there and fix the path`);
  31. const errs = file(path);
  32. if (errs.length) out.push(`${where}: ${field} ${path}: ${errs.join('; ')} → ${fix(path)}`);
  33. }
  34. if (n.active) {
  35. for (const p of ic.pages()) {
  36. const own = ownIcons(p);
  37. ref(p.id, 'icon', own.icon, true);
  38. ref(p.id, 'art', own.art, false);
  39. }
  40. for (const c of Object.values(ic.collections())) ref(c.coll + '.md', 'icons.item', c.item, true);
  41. const walk = (rel: string) => {
  42. const abs = join(ic.root, rel.slice(1));
  43. if (!existsSync(abs)) return;
  44. for (const name of readdirSync(abs).sort()) {
  45. const r = rel + '/' + name;
  46. if (statSync(join(abs, name)).isDirectory()) walk(r);
  47. else if (name.toLowerCase().endsWith('.svg') && !checked.has(r)) {
  48. const errs = file(r);
  49. if (errs.length) out.push(`${r}: ${errs.join('; ')} → ${fix(r)}`);
  50. }
  51. }
  52. };
  53. walk(ic.s.folder);
  54. }
  55. finish(n, out);