ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-icons/actions/import.ts

actions/import.ts

38 行, 2.5 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // shtuk run shtukbase-icons import <file.svg> --name <name> [--write]
  2. // Brings a drawing into the set without redrawing it: groups dissolved, style attributes made attributes, an app tile
  3. // dropped (white lines on it take its colour), every colour snapped to the palette of style 79. Prints the result and
  4. // what changed; --write puts it in /design/shtukbase-icons/flat/<name>.svg and prints the record to add to the registry.
  5. import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
  6. import { dirname, join, resolve } from 'node:path';
  7. import { search } from '../../icons/lib/registry.ts';
  8. import { checkFlat } from '../lib/style-check.ts';
  9. import { BASE_STYLE, R, SET_DIR, registry, styleById } from '../lib/sbicons.ts';
  10. const root = process.cwd();
  11. const args = process.argv.slice(2);
  12. const file = args.find((a, i) => !a.startsWith('-') && args[i - 1] !== '--name');
  13. const ni = args.indexOf('--name');
  14. const name = ni >= 0 ? args[ni + 1] : '';
  15. if (!file || !name || !/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(name)) {
  16. console.log('usage: shtuk run shtukbase-icons import <file.svg> --name <kebab-name> [--write]');
  17. process.exit(2);
  18. }
  19. const style = styleById(root, BASE_STYLE);
  20. const { svg, notes } = R.canonical(readFileSync(resolve(root, file), 'utf8'), style.rawPalette);
  21. for (const n of notes) console.log(`- ${n}`);
  22. const left = checkFlat(svg, style.rawPalette);
  23. for (const f of left) console.log(`! ${f}`);
  24. const reg = registry(root);
  25. if (reg.icons.some((r) => r.name === name)) console.log(`! the name "${name}" is taken in the registry: one meaning, one icon`);
  26. const near = search(reg, name.replace(/-/g, ' '), 5);
  27. if (near.length) console.log(`Near by meaning (reuse one if it means the same): ${near.map((h) => `${h.rec.name} (${h.rec.description})`).join('; ')}`);
  28. const target = `${SET_DIR}/flat/${name}.svg`;
  29. if (args.includes('--write')) {
  30. const a = join(root, target.slice(1));
  31. if (existsSync(a)) { console.log(`! ${target} exists; not overwritten`); process.exit(1); }
  32. mkdirSync(dirname(a), { recursive: true });
  33. writeFileSync(a, svg);
  34. console.log(`wrote ${target}. Add its record to ${SET_DIR}/registry.json:`);
  35. console.log(JSON.stringify({ name, file: target, description: '<one line: what it draws>', use: '<where it fits>', avoid: '<where it does not>', aliases: [], group: '<colour group>', added: { date: new Date().toISOString().slice(0, 10), by: '<you>', from: file, why: '<why the near ones did not fit>' } }, null, 2));
  36. console.log('Then: shtuk run shtukbase-icons build, shtuk run icons lens, shtuk check.');
  37. } else console.log(svg);