archivo del paquete→ .shtukbase/packages/design-components/lib/health.ts
lib/health.ts
82 líneas, 4.4 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The health of a component library against the convention /design/conventions/component-library.md, as bands:
- // missing a component file whose header does not parse; a CSS file with a raw colour (tokens only)
- // fallbacks components of the shadcn list the library does not have (a project takes them from another library)
- // extra components of its own (ids outside the shadcn list); CSS names read from the style that the contract does not know
- // Each library is judged by its own folder, on its own files (a component id that two libraries share counts in both).
- // Reads files, writes nothing. The working directory is the project root.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { readContract } from '../../design-tokens/lib/contract.ts';
- import { rawColours } from './css.ts';
- import { findHeader, kindOf, parseHeader } from './header.ts';
- import { type Place, collect, places, settings, walk } from './project.ts';
- import shadcn from './shadcn-names.json';
- export const SHADCN: string[] = shadcn.names;
- export type LibHealth = { lib: string; components: number; missing: string[]; fallbacks: string[]; extra: string[] };
- /** CSS custom property names a text reads with var() and the ones it declares. */
- function names(css: string): { read: Set<string>; declared: Set<string> } {
- const text = css.replace(/\/\*[\s\S]*?\*\//g, '');
- return {
- read: new Set([...text.matchAll(/var\(\s*(--[\w-]+)/g)].map((m) => m[1])),
- declared: new Set([...text.matchAll(/(--[\w-]+)\s*:/g)].map((m) => m[1])),
- };
- }
- /** The libraries to judge: the ones chosen on /design/design.md, else every place that holds components. `only` names one. */
- export function libraries(root: string, only?: string): Place[] {
- const s = settings(root);
- const all = places(root, s);
- if (only) return all.filter((p) => p.lib === only);
- return s.libraries.length ? all.filter((p) => s.libraries.includes(p.lib)) : all;
- }
- export function libHealth(root: string, p: Place): LibHealth {
- const contract = readContract(root);
- const files: string[] = [];
- walk(root, p.dir, files);
- const missing: string[] = [], ids = new Set<string>(), own = new Set<string>(), read = new Set<string>();
- for (const f of files) {
- const text = readFileSync(join(root, f), 'utf8');
- if (f.endsWith('.css')) {
- if (rawColours(text).length) missing.push(`${f} (raw colour)`);
- const n = names(text);
- n.read.forEach((x) => read.add(x));
- n.declared.forEach((x) => own.add(x));
- } else if (kindOf(f) && findHeader(text)) {
- const h = parseHeader(text, f).header;
- if (h) ids.add(h.id);
- else missing.push(`${f} (header)`);
- }
- }
- const known = new Set([...contract.required, ...Object.keys(contract.extension)]);
- // `--<component-id>-<control>` custom properties are set from outside by the controls (see the library page): the component's own, not the style's
- const control = (n: string) => [...ids].some((id) => n.startsWith(`--${id}-`));
- const outside = [...read].filter((n) => !own.has(n) && !known.has(n) && !control(n)).sort();
- const extra = [...ids].filter((id) => !SHADCN.includes(id)).sort();
- return {
- lib: p.lib, components: ids.size, missing,
- fallbacks: SHADCN.filter((n) => !ids.has(n)),
- extra: [...(extra.length ? [`components ${extra.join(', ')}`] : []), ...(outside.length ? [`css names ${outside.join(', ')}`] : [])],
- };
- }
- /** The lines of the check, one set per library: `health component-library/<lib>: <band>[: <names>]`. */
- export function healthLines(root = process.cwd(), only?: string): string[] {
- const out: string[] = [];
- for (const p of libraries(root, only)) {
- if (!existsSync(join(root, p.dir))) continue;
- const h = libHealth(root, p);
- const at = `health component-library/${h.lib}:`;
- if (h.missing.length) out.push(`${at} missing: ${h.missing.join(', ')}`);
- if (h.fallbacks.length) out.push(`${at} fallbacks: ${h.fallbacks.join(', ')} (${h.fallbacks.length} of ${SHADCN.length} shadcn components are not in this library)`);
- if (!h.missing.length && !h.fallbacks.length) out.push(`${at} full (${h.components} components)`);
- for (const e of h.extra) out.push(`${at} extra: ${e}`);
- }
- for (const { library, message } of collect(root).overrideWarnings) {
- if (!only || library === only) out.push(`health component-library/${library}: override: ${message}`);
- }
- return out;
- }