ShtukbaseStandard
包文件→ .shtukbase/packages/shadcn/checks/config.ts

checks/config.ts

61 行, 3.8 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // SHD-R01. components.json is valid: it is a JSON object with the fields the shadcn CLI reads, of the right types —
  2. // style (string), rsc and tsx (boolean), tailwind {css (string, the file exists), baseColor (string), cssVariables
  3. // (boolean), config and prefix (strings, may be empty)}, aliases {components, utils (required), ui, lib, hooks:
  4. // strings that start with the same prefix}, iconLibrary (string, optional); $schema, if given, is the shadcn schema.
  5. // When components were placed (.shadcn.json), the `ui` alias resolves to a folder and the `utils` alias to a file.
  6. import { existsSync, statSync } from 'node:fs';
  7. import { join } from 'node:path';
  8. import { report } from '../../design-base/lib/files.ts';
  9. import { CONFIG, SCHEMA_URL, aliases, aliasPath, components, readManifest, targets } from '../actions/shadcnlib.ts';
  10. export const RULE = { id: 'shd-r01', title: 'components.json is valid' };
  11. const KNOWN = new Set(['$schema', 'style', 'rsc', 'tsx', 'tailwind', 'aliases', 'iconLibrary', 'registries', 'menuColor', 'menuAccent', 'rtl']);
  12. export function check(root = process.cwd()): string[] {
  13. const placed = readManifest(root);
  14. if (!existsSync(join(root, CONFIG))) return placed ? [`${CONFIG}: the file is missing but the components are placed — restore it: shtuk run shadcn place`] : [];
  15. const { cfg, error } = components(root);
  16. if (!cfg) return [`${CONFIG}: ${error}`];
  17. const out: string[] = [];
  18. const bad = (what: string) => out.push(`${CONFIG}: ${what}`);
  19. const type = (v: unknown, t: string, name: string, required = true) => {
  20. if (v === undefined) { if (required) bad(`no field ${name} (${t})`); return; }
  21. if (typeof v !== t) bad(`field ${name} must be ${t === 'string' ? 'a string' : 'true or false'}, but it is ${JSON.stringify(v)}`);
  22. };
  23. if (cfg.$schema !== undefined && cfg.$schema !== SCHEMA_URL) bad(`$schema is ${JSON.stringify(cfg.$schema)}, expected ${SCHEMA_URL}`);
  24. type(cfg.style, 'string', 'style');
  25. type(cfg.rsc, 'boolean', 'rsc');
  26. type(cfg.tsx, 'boolean', 'tsx');
  27. type(cfg.iconLibrary, 'string', 'iconLibrary', false);
  28. for (const k of Object.keys(cfg)) if (!KNOWN.has(k)) bad(`unknown field ${k} — a typo? (schema: ${SCHEMA_URL})`);
  29. const tw = cfg.tailwind;
  30. if (!tw || typeof tw !== 'object') bad('no tailwind object');
  31. else {
  32. type(tw.css, 'string', 'tailwind.css');
  33. type(tw.baseColor, 'string', 'tailwind.baseColor');
  34. type(tw.cssVariables, 'boolean', 'tailwind.cssVariables');
  35. type(tw.config, 'string', 'tailwind.config', false);
  36. type(tw.prefix, 'string', 'tailwind.prefix', false);
  37. if (typeof tw.css === 'string' && !existsSync(join(root, tw.css))) bad(`tailwind.css: file ${tw.css} does not exist`);
  38. if (tw.cssVariables === false) bad('tailwind.cssVariables: false — the theme relies on CSS variables, it must be true');
  39. }
  40. const al = cfg.aliases;
  41. if (!al || typeof al !== 'object') bad('no aliases object');
  42. else {
  43. type(al.components, 'string', 'aliases.components');
  44. type(al.utils, 'string', 'aliases.utils');
  45. for (const k of ['ui', 'lib', 'hooks']) type(al[k], 'string', `aliases.${k}`, false);
  46. const prefixes = new Set(Object.values(al).filter((v) => typeof v === 'string').map((v) => /^([@~#][\w-]*)\//.exec(v)?.[1] ?? '(no prefix)'));
  47. if (prefixes.size > 1) bad(`aliases with different prefixes (${[...prefixes].join(', ')}) — all must have the same one`);
  48. }
  49. if (placed && !out.length) {
  50. const t = targets(cfg, root);
  51. const ui = join(root, t.ui);
  52. if (!existsSync(ui) || !statSync(ui).isDirectory()) bad(`aliases.ui ${aliases(cfg).ui} → ${t.ui}: no such folder`);
  53. if (!existsSync(join(root, t.utils))) bad(`aliases.utils ${aliases(cfg).utils} → ${t.utils}: no such file (aliasRoot: ${aliasPath('@/', root) || '.'})`);
  54. }
  55. return out;
  56. }
  57. if (import.meta.main) report(check());