ShtukbaseStandard
package file→ .shtukbase/packages/icons/actions/validate.ts

actions/validate.ts

33 lines, 1.7 KB.

  1. // shtuk run icons validate <file.svg | folder>...: checks SVG icons by the contract and the installed icon style.
  2. // Prints one line per finding; exit 1 if there is an error. Warnings do not fail.
  3. import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { iconStyle, settings } from '../lib/iconlib.ts';
  6. import { validate } from '../lib/svg.ts';
  7. const root = process.cwd();
  8. const args = process.argv.slice(2);
  9. if (!args.length) {
  10. console.log('usage: shtuk run icons validate <file.svg | folder>... (paths from the project root)');
  11. process.exit(2);
  12. }
  13. const s = settings(root);
  14. const style = iconStyle(root, s);
  15. const files: string[] = [];
  16. const add = (p: string) => {
  17. const abs = join(root, p.replace(/^\/+/, ''));
  18. if (!existsSync(abs)) return void files.push(p);
  19. if (statSync(abs).isDirectory()) for (const n of readdirSync(abs).sort()) add(join(p, n));
  20. else if (p.toLowerCase().endsWith('.svg')) files.push(p);
  21. };
  22. args.forEach(add);
  23. let errors = 0;
  24. console.log(`contract: square viewBox, at most ${s.maxBytes} bytes, safe elements only${style ? `; style ${style.page} (${style.rules.colors?.length ?? 0} colours${style.rules.viewBox ? ', viewBox ' + style.rules.viewBox : ''})` : '; no icon style installed'}`);
  25. for (const f of files) {
  26. const abs = join(root, f.replace(/^\/+/, ''));
  27. if (!existsSync(abs)) { console.log(`${f}: error: the file does not exist`); errors++; continue; }
  28. const found = validate(readFileSync(abs, 'utf8'), s.maxBytes, style?.rules);
  29. errors += found.filter((x) => x.level === 'error').length;
  30. console.log(found.length ? found.map((x) => `${f}: ${x.level}: ${x.text}`).join('\n') : `${f}: ok`);
  31. }
  32. process.exitCode = errors ? 1 : 0;