ShtukbaseStandard
файл пакета→ .shtukbase/packages/shadcn/checks/theme.ts

checks/theme.ts

57 строк, 3.2 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // SHD-R04. The theme has both schemes and every required variable. In the project's CSS: the light scheme (:root)
  2. // defines --radius and every required color variable; a dark scheme (.dark, [data-theme="dark"] or
  3. // prefers-color-scheme: dark) exists and defines every required color variable again; each required color resolves
  4. // to a color (after var() substitution) in both schemes. A value the reader cannot evaluate (color-mix(), a gradient)
  5. // counts as defined.
  6. import { existsSync } from 'node:fs';
  7. import { join } from 'node:path';
  8. import { parseColor } from '../../design-base/lib/color.ts';
  9. import { parseCss, resolveVars, themeOf, themes } from '../../design-base/lib/css.ts';
  10. import { projectFiles, read, report } from '../../design-base/lib/files.ts';
  11. import { components, readManifest } from '../actions/shadcnlib.ts';
  12. export const RULE = { id: 'shd-r04', title: 'The theme has both schemes and all required variables' };
  13. export const REQUIRED = [
  14. 'background', 'foreground', 'card', 'card-foreground', 'popover', 'popover-foreground', 'primary', 'primary-foreground',
  15. 'secondary', 'secondary-foreground', 'muted', 'muted-foreground', 'accent', 'accent-foreground', 'destructive',
  16. 'border', 'input', 'ring',
  17. ].map((n) => `--${n}`);
  18. export function check(root = process.cwd()): string[] {
  19. const { cfg } = components(root);
  20. if (!cfg && !readManifest(root)) return []; // shadcn not used in this project yet
  21. const themeFile = cfg?.tailwind?.css ?? 'components.json → tailwind.css';
  22. const files = projectFiles(['css', 'scss']).map((rel) => ({ rel, text: read(rel, root) }));
  23. if (!files.length) return [`${themeFile}: the project has no CSS with a theme — place it: shtuk run shadcn place`];
  24. const { themes: all } = themes(files);
  25. const base = all.get('base') ?? new Map();
  26. const out: string[] = [];
  27. const where = existsSync(join(root, String(themeFile))) ? String(themeFile) : files[0].rel;
  28. for (const v of [...REQUIRED, '--radius']) if (!base.has(v)) out.push(`${where}: :root has no ${v}`);
  29. // The dark scheme's own declarations (not inherited from :root).
  30. const darkOwn = new Set<string>();
  31. for (const f of files) {
  32. for (const b of parseCss(f.text)) {
  33. if (b.parents.length) continue;
  34. const media = b.at.some((a) => /prefers-color-scheme\s*:\s*dark/.test(a));
  35. if (b.selectors.some((s) => themeOf(s, media) === 'dark')) for (const d of b.decls) if (d.prop.startsWith('--')) darkOwn.add(d.prop);
  36. }
  37. }
  38. const dark = all.get('dark');
  39. if (!dark) out.push(`${where}: no dark scheme (.dark { … }) — set all theme colors for dark`);
  40. else for (const v of REQUIRED) if (!darkOwn.has(v)) out.push(`${where}: .dark has no ${v}`);
  41. for (const [name, vars] of [['light', base], ['dark', dark]] as const) {
  42. if (!vars) continue;
  43. for (const v of REQUIRED) {
  44. if (!vars.has(v)) continue;
  45. const val = resolveVars(vars.get(v) as string, vars);
  46. if (val === null) { out.push(`${where}: ${name} scheme: ${v} refers to a variable that does not exist`); continue; }
  47. if (!parseColor(val) && !/^[\w-]+\(/.test(val.trim()) && !/^[\d.]+\s+[\d.]+%?\s+[\d.]+%?/.test(val.trim()))
  48. out.push(`${where}: ${name} scheme: ${v}: ${val} — not a color`);
  49. }
  50. }
  51. return out;
  52. }
  53. if (import.meta.main) report(check());