包文件→ .shtukbase/packages/icons/actions/validate.ts
actions/validate.ts
33 行, 1.7 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // shtuk run icons validate <file.svg | folder>...: checks SVG icons by the contract and the installed icon style.
- // Prints one line per finding; exit 1 if there is an error. Warnings do not fail.
- import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- import { iconStyle, settings } from '../lib/iconlib.ts';
- import { validate } from '../lib/svg.ts';
- const root = process.cwd();
- const args = process.argv.slice(2);
- if (!args.length) {
- console.log('usage: shtuk run icons validate <file.svg | folder>... (paths from the project root)');
- process.exit(2);
- }
- const s = settings(root);
- const style = iconStyle(root, s);
- const files: string[] = [];
- const add = (p: string) => {
- const abs = join(root, p.replace(/^\/+/, ''));
- if (!existsSync(abs)) return void files.push(p);
- if (statSync(abs).isDirectory()) for (const n of readdirSync(abs).sort()) add(join(p, n));
- else if (p.toLowerCase().endsWith('.svg')) files.push(p);
- };
- args.forEach(add);
- let errors = 0;
- 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'}`);
- for (const f of files) {
- const abs = join(root, f.replace(/^\/+/, ''));
- if (!existsSync(abs)) { console.log(`${f}: error: the file does not exist`); errors++; continue; }
- const found = validate(readFileSync(abs, 'utf8'), s.maxBytes, style?.rules);
- errors += found.filter((x) => x.level === 'error').length;
- console.log(found.length ? found.map((x) => `${f}: ${x.level}: ${x.text}`).join('\n') : `${f}: ok`);
- }
- process.exitCode = errors ? 1 : 0;