ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/icons/lib/lensdata.ts

lib/lensdata.ts

44 líneas, 2.1 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // The data of the icon lens (/design/icons.html): every icon set (its registry and its flat drawings) and every
  2. // installed icon style, as `window.SB_ICONS` in lib/design/icons.data.js; the renderer goes next to it as
  3. // lib/design/icon-render.js, so the lens draws each style itself.
  4. import { existsSync, readFileSync } from 'node:fs';
  5. import { join } from 'node:path';
  6. import { iconStyles, settings } from './iconlib.ts';
  7. import { loadSets } from './registry.ts';
  8. export const DATA_FILE = 'lib/design/icons.data.js';
  9. export const RENDER_FILE = 'lib/design/icon-render.js';
  10. export type LensData = {
  11. version: 1;
  12. style: string; // the id of the style the project holds
  13. styles: { id: string; title: string; page: string; role: string; set: string; render: Record<string, unknown> | null; palette: Record<string, unknown> }[];
  14. sets: { id: string; registry: string; prefix: string; icons: Record<string, unknown>[] }[];
  15. errors: string[];
  16. };
  17. export function lensData(root: string): LensData {
  18. const s = settings(root);
  19. const styles = iconStyles(root, s);
  20. const { sets, errors } = loadSets(root);
  21. const read = (p: string) => { const a = join(root, p.replace(/^\/+/, '')); return existsSync(a) ? readFileSync(a, 'utf8') : null; };
  22. return {
  23. version: 1,
  24. style: styles[0]?.id ?? '', // iconStyles puts the held style first
  25. styles: styles.map((x) => ({ id: x.id, title: x.title, page: x.page, role: x.role, set: x.set, render: x.render, palette: x.rawPalette })),
  26. sets: sets.map((set) => ({
  27. id: set.id, registry: set.path, prefix: set.registry.prefix || set.id,
  28. icons: set.registry.icons.map((r) => {
  29. const svg = r.file ? read(r.file) : null;
  30. if (r.file && svg === null) errors.push(`${set.path}: "${r.name}": no file ${r.file}`);
  31. return { ...r, svg: svg ?? '' };
  32. }),
  33. })),
  34. errors,
  35. };
  36. }
  37. /** The text of lib/design/icons.data.js. */
  38. export function dataText(d: LensData): string {
  39. return `// Written by \`shtuk run icons lens\` (package icons): the icon sets and the icon styles for /design/icons.html.\nwindow.SB_ICONS = ${JSON.stringify(d)};\n`;
  40. }