archivo del paquete→ .shtukbase/packages/shadcn/checks/theme.ts
checks/theme.ts
57 líneas, 3.2 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // SHD-R04. The theme has both schemes and every required variable. In the project's CSS: the light scheme (:root)
- // defines --radius and every required color variable; a dark scheme (.dark, [data-theme="dark"] or
- // prefers-color-scheme: dark) exists and defines every required color variable again; each required color resolves
- // to a color (after var() substitution) in both schemes. A value the reader cannot evaluate (color-mix(), a gradient)
- // counts as defined.
- import { existsSync } from 'node:fs';
- import { join } from 'node:path';
- import { parseColor } from '../../design-base/lib/color.ts';
- import { parseCss, resolveVars, themeOf, themes } from '../../design-base/lib/css.ts';
- import { projectFiles, read, report } from '../../design-base/lib/files.ts';
- import { components, readManifest } from '../actions/shadcnlib.ts';
- export const RULE = { id: 'shd-r04', title: 'The theme has both schemes and all required variables' };
- export const REQUIRED = [
- 'background', 'foreground', 'card', 'card-foreground', 'popover', 'popover-foreground', 'primary', 'primary-foreground',
- 'secondary', 'secondary-foreground', 'muted', 'muted-foreground', 'accent', 'accent-foreground', 'destructive',
- 'border', 'input', 'ring',
- ].map((n) => `--${n}`);
- export function check(root = process.cwd()): string[] {
- const { cfg } = components(root);
- if (!cfg && !readManifest(root)) return []; // shadcn not used in this project yet
- const themeFile = cfg?.tailwind?.css ?? 'components.json → tailwind.css';
- const files = projectFiles(['css', 'scss']).map((rel) => ({ rel, text: read(rel, root) }));
- if (!files.length) return [`${themeFile}: the project has no CSS with a theme — place it: shtuk run shadcn place`];
- const { themes: all } = themes(files);
- const base = all.get('base') ?? new Map();
- const out: string[] = [];
- const where = existsSync(join(root, String(themeFile))) ? String(themeFile) : files[0].rel;
- for (const v of [...REQUIRED, '--radius']) if (!base.has(v)) out.push(`${where}: :root has no ${v}`);
- // The dark scheme's own declarations (not inherited from :root).
- const darkOwn = new Set<string>();
- for (const f of files) {
- for (const b of parseCss(f.text)) {
- if (b.parents.length) continue;
- const media = b.at.some((a) => /prefers-color-scheme\s*:\s*dark/.test(a));
- if (b.selectors.some((s) => themeOf(s, media) === 'dark')) for (const d of b.decls) if (d.prop.startsWith('--')) darkOwn.add(d.prop);
- }
- }
- const dark = all.get('dark');
- if (!dark) out.push(`${where}: no dark scheme (.dark { … }) — set all theme colors for dark`);
- else for (const v of REQUIRED) if (!darkOwn.has(v)) out.push(`${where}: .dark has no ${v}`);
- for (const [name, vars] of [['light', base], ['dark', dark]] as const) {
- if (!vars) continue;
- for (const v of REQUIRED) {
- if (!vars.has(v)) continue;
- const val = resolveVars(vars.get(v) as string, vars);
- if (val === null) { out.push(`${where}: ${name} scheme: ${v} refers to a variable that does not exist`); continue; }
- if (!parseColor(val) && !/^[\w-]+\(/.test(val.trim()) && !/^[\d.]+\s+[\d.]+%?\s+[\d.]+%?/.test(val.trim()))
- out.push(`${where}: ${name} scheme: ${v}: ${val} — not a color`);
- }
- }
- return out;
- }
- if (import.meta.main) report(check());