package file→ .shtukbase/packages/shadcn/checks/config.ts
checks/config.ts
61 lines, 3.8 KB.
- // SHD-R01. components.json is valid: it is a JSON object with the fields the shadcn CLI reads, of the right types —
- // style (string), rsc and tsx (boolean), tailwind {css (string, the file exists), baseColor (string), cssVariables
- // (boolean), config and prefix (strings, may be empty)}, aliases {components, utils (required), ui, lib, hooks:
- // strings that start with the same prefix}, iconLibrary (string, optional); $schema, if given, is the shadcn schema.
- // When components were placed (.shadcn.json), the `ui` alias resolves to a folder and the `utils` alias to a file.
- import { existsSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- import { report } from '../../design-base/lib/files.ts';
- import { CONFIG, SCHEMA_URL, aliases, aliasPath, components, readManifest, targets } from '../actions/shadcnlib.ts';
- export const RULE = { id: 'shd-r01', title: 'components.json is valid' };
- const KNOWN = new Set(['$schema', 'style', 'rsc', 'tsx', 'tailwind', 'aliases', 'iconLibrary', 'registries', 'menuColor', 'menuAccent', 'rtl']);
- export function check(root = process.cwd()): string[] {
- const placed = readManifest(root);
- if (!existsSync(join(root, CONFIG))) return placed ? [`${CONFIG}: the file is missing but the components are placed — restore it: shtuk run shadcn place`] : [];
- const { cfg, error } = components(root);
- if (!cfg) return [`${CONFIG}: ${error}`];
- const out: string[] = [];
- const bad = (what: string) => out.push(`${CONFIG}: ${what}`);
- const type = (v: unknown, t: string, name: string, required = true) => {
- if (v === undefined) { if (required) bad(`no field ${name} (${t})`); return; }
- if (typeof v !== t) bad(`field ${name} must be ${t === 'string' ? 'a string' : 'true or false'}, but it is ${JSON.stringify(v)}`);
- };
- if (cfg.$schema !== undefined && cfg.$schema !== SCHEMA_URL) bad(`$schema is ${JSON.stringify(cfg.$schema)}, expected ${SCHEMA_URL}`);
- type(cfg.style, 'string', 'style');
- type(cfg.rsc, 'boolean', 'rsc');
- type(cfg.tsx, 'boolean', 'tsx');
- type(cfg.iconLibrary, 'string', 'iconLibrary', false);
- for (const k of Object.keys(cfg)) if (!KNOWN.has(k)) bad(`unknown field ${k} — a typo? (schema: ${SCHEMA_URL})`);
- const tw = cfg.tailwind;
- if (!tw || typeof tw !== 'object') bad('no tailwind object');
- else {
- type(tw.css, 'string', 'tailwind.css');
- type(tw.baseColor, 'string', 'tailwind.baseColor');
- type(tw.cssVariables, 'boolean', 'tailwind.cssVariables');
- type(tw.config, 'string', 'tailwind.config', false);
- type(tw.prefix, 'string', 'tailwind.prefix', false);
- if (typeof tw.css === 'string' && !existsSync(join(root, tw.css))) bad(`tailwind.css: file ${tw.css} does not exist`);
- if (tw.cssVariables === false) bad('tailwind.cssVariables: false — the theme relies on CSS variables, it must be true');
- }
- const al = cfg.aliases;
- if (!al || typeof al !== 'object') bad('no aliases object');
- else {
- type(al.components, 'string', 'aliases.components');
- type(al.utils, 'string', 'aliases.utils');
- for (const k of ['ui', 'lib', 'hooks']) type(al[k], 'string', `aliases.${k}`, false);
- const prefixes = new Set(Object.values(al).filter((v) => typeof v === 'string').map((v) => /^([@~#][\w-]*)\//.exec(v)?.[1] ?? '(no prefix)'));
- if (prefixes.size > 1) bad(`aliases with different prefixes (${[...prefixes].join(', ')}) — all must have the same one`);
- }
- if (placed && !out.length) {
- const t = targets(cfg, root);
- const ui = join(root, t.ui);
- if (!existsSync(ui) || !statSync(ui).isDirectory()) bad(`aliases.ui ${aliases(cfg).ui} → ${t.ui}: no such folder`);
- if (!existsSync(join(root, t.utils))) bad(`aliases.utils ${aliases(cfg).utils} → ${t.utils}: no such file (aliasRoot: ${aliasPath('@/', root) || '.'})`);
- }
- return out;
- }
- if (import.meta.main) report(check());