файл пакета→ .shtukbase/packages/icons/actions/sanitize.ts
actions/sanitize.ts
29 строк, 1.7 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // shtuk run icons sanitize <file.svg> [--write | -o <out.svg>]: drops what the contract refuses (scripts, handlers,
- // external links, text, editor metadata, currentColor), keeps the drawing, adds xmlns and a square viewBox when it can.
- // Prints the result (or writes it), then validates it: exit 1 if errors remain (a wrong viewBox or size needs a redraw).
- import { existsSync, readFileSync, writeFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { iconStyle, settings } from '../lib/iconlib.ts';
- import { sanitize, validate } from '../lib/svg.ts';
- const root = process.cwd();
- const args = process.argv.slice(2);
- const file = args.find((a) => !a.startsWith('-') && args[args.indexOf(a) - 1] !== '-o');
- if (!file) {
- console.log('usage: shtuk run icons sanitize <file.svg> [--write | -o <out.svg>]');
- process.exit(2);
- }
- const abs = join(root, file.replace(/^\/+/, ''));
- if (!existsSync(abs)) { console.error(`${file}: the file does not exist`); process.exit(1); }
- const before = readFileSync(abs, 'utf8');
- const after = sanitize(before);
- const oi = args.indexOf('-o');
- const dest = args.includes('--write') ? abs : oi >= 0 && args[oi + 1] ? join(root, args[oi + 1]) : '';
- if (dest) writeFileSync(dest, after);
- else process.stdout.write(after);
- const s = settings(root);
- const left = validate(after, s.maxBytes, iconStyle(root, s)?.rules).filter((f) => f.level === 'error');
- const note = (t: string) => (dest ? console.log(t) : console.error(t));
- note(`${file}: ${before.length} → ${after.length} bytes${dest ? ', written to ' + dest.slice(root.length + 1) : ''}`);
- for (const f of left) note(`${file}: still an error: ${f.text}`);
- process.exitCode = left.length ? 1 : 0;