файл пакета→ .shtukbase/packages/shtukbase-icons/lib/sbicons.ts
lib/sbicons.ts
74 строки, 4.2 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The Shtukbase icon set in a project: its registry, its flat drawings, its IconStyle pages, and what the build makes of
- // them (the set rendered in a style, the Iconify JSON). Reads from the project root; the renderer is the icons package's.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { type IconStyle, iconStylePages, readIconStyle, settings } from '../../icons/lib/iconlib.ts';
- import type { IconRecord, Registry } from '../../icons/lib/registry.ts';
- // lib/render.js is plain JS shared with the browser (it sets module.exports under Bun).
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- export const R: {
- render(src: string, params: Record<string, unknown> | null, palette: Record<string, unknown>, opts?: { idPrefix?: string }): string;
- canonical(src: string, palette: Record<string, unknown>): { svg: string; notes: string[] };
- parse(src: string): { root: [string, string][]; els: { tag: string; attrs: [string, string][] }[] };
- snap(c: string, palette: Record<string, unknown>): { fill: string; hue: string | null; tone: string } | null;
- toneOf(c: string, palette: Record<string, unknown>): { fill: string; hue: string | null; tone: string } | null;
- flatColors(palette: Record<string, unknown>): string[];
- norm(c: string): string;
- } = require('../../icons/lib/render.js');
- export const SET_DIR = '/design/shtukbase-icons';
- export const REGISTRY = `${SET_DIR}/registry.json`;
- export const ICONIFY = `${SET_DIR}/iconify.json`;
- export const BASE_STYLE = '79';
- const abs = (root: string, p: string) => join(root, p.replace(/^\/+/, ''));
- export const readText = (root: string, p: string): string | null => (existsSync(abs(root, p)) ? readFileSync(abs(root, p), 'utf8') : null);
- export function registry(root: string): Registry {
- const t = readText(root, REGISTRY);
- if (t === null) throw new Error(`${REGISTRY}: not found (is shtukbase-icons installed?)`);
- return JSON.parse(t) as Registry;
- }
- /** The styles of this set: the IconStyle pages under /design/shtukbase-icons/styles/ (or that name this registry). */
- export function setStyles(root: string): IconStyle[] {
- return iconStylePages(root).map((p) => readIconStyle(root, p)).filter((s): s is IconStyle => !!s && (s.set === REGISTRY || s.page.startsWith(`${SET_DIR}/styles/`)));
- }
- export function styleById(root: string, id: string): IconStyle {
- const all = setStyles(root);
- const s = all.find((x) => x.id === id);
- if (!s) throw new Error(`no icon style "${id}" in ${SET_DIR}/styles/ (have: ${all.map((x) => x.id).join(', ') || 'none'})`);
- if (!s.render) throw new Error(`${s.page}: no render block`);
- return s;
- }
- /** One icon rendered in a style. */
- export function renderIcon(root: string, rec: IconRecord, style: IconStyle, idPrefix = ''): string {
- const src = readText(root, rec.file);
- if (src === null) throw new Error(`${rec.file}: not found`);
- return R.render(src, style.render, style.rawPalette, idPrefix ? { idPrefix } : undefined);
- }
- /** The folder the rendered set goes to: the `set` setting of the icons package (default /assets/icons/set). */
- export const outDir = (root: string) => settings(root).set;
- /** The set as an Iconify icon set (https://iconify.design/docs/types/iconify-json.html), rendered in a style. */
- export function iconify(root: string, reg: Registry, style: IconStyle): Record<string, unknown> {
- const icons: Record<string, { body: string }> = {};
- const aliases: Record<string, { parent: string }> = {};
- const names = new Set(reg.icons.map((r) => r.name));
- for (const rec of reg.icons) {
- const svg = renderIcon(root, rec, style, `${rec.name}-`);
- const body = svg.replace(/^[\s\S]*?<svg[^>]*>/, '').replace(/<\/svg>\s*$/, '');
- icons[rec.name] = { body };
- for (const a of rec.aliases ?? []) if (/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(a) && !names.has(a) && !aliases[a]) aliases[a] = { parent: rec.name };
- }
- return {
- prefix: reg.prefix || 'sb',
- info: { name: 'Shtukbase icons', total: reg.icons.length, author: { name: 'Shtukbase' }, height: 96, palette: true, category: `style ${style.id}` },
- lastModified: 0,
- icons, aliases, width: 96, height: 96,
- };
- }
- export const iconifyText = (j: Record<string, unknown>) => JSON.stringify(j, null, 1) + '\n';