包文件→ .shtukbase/packages/icons/lib/lensdata.ts
lib/lensdata.ts
44 行, 2.1 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // The data of the icon lens (/design/icons.html): every icon set (its registry and its flat drawings) and every
- // installed icon style, as `window.SB_ICONS` in lib/design/icons.data.js; the renderer goes next to it as
- // lib/design/icon-render.js, so the lens draws each style itself.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { iconStyles, settings } from './iconlib.ts';
- import { loadSets } from './registry.ts';
- export const DATA_FILE = 'lib/design/icons.data.js';
- export const RENDER_FILE = 'lib/design/icon-render.js';
- export type LensData = {
- version: 1;
- style: string; // the id of the style the project holds
- styles: { id: string; title: string; page: string; role: string; set: string; render: Record<string, unknown> | null; palette: Record<string, unknown> }[];
- sets: { id: string; registry: string; prefix: string; icons: Record<string, unknown>[] }[];
- errors: string[];
- };
- export function lensData(root: string): LensData {
- const s = settings(root);
- const styles = iconStyles(root, s);
- const { sets, errors } = loadSets(root);
- const read = (p: string) => { const a = join(root, p.replace(/^\/+/, '')); return existsSync(a) ? readFileSync(a, 'utf8') : null; };
- return {
- version: 1,
- style: styles[0]?.id ?? '', // iconStyles puts the held style first
- 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 })),
- sets: sets.map((set) => ({
- id: set.id, registry: set.path, prefix: set.registry.prefix || set.id,
- icons: set.registry.icons.map((r) => {
- const svg = r.file ? read(r.file) : null;
- if (r.file && svg === null) errors.push(`${set.path}: "${r.name}": no file ${r.file}`);
- return { ...r, svg: svg ?? '' };
- }),
- })),
- errors,
- };
- }
- /** The text of lib/design/icons.data.js. */
- export function dataText(d: LensData): string {
- 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`;
- }