package file→ .shtukbase/packages/shadcn/checks/folder.ts
checks/folder.ts
34 lines, 1.8 KB.
- // SHD-R02. Components live in one folder — the `ui` alias of components.json:
- // every component placed (.shadcn.json) is there; a file elsewhere does not define a ui component (data-slot="…")
- // and does not import Radix primitives (radix-ui, @radix-ui/*) — screens use the components of the folder.
- import { existsSync } from 'node:fs';
- import { join } from 'node:path';
- import { lineAt, projectFiles, read, report } from '../../design-base/lib/files.ts';
- import { components, index, readManifest, targets } from '../actions/shadcnlib.ts';
- export const RULE = { id: 'shd-r02', title: 'Components in one folder' };
- export function check(root = process.cwd()): string[] {
- const placed = readManifest(root);
- const { cfg } = components(root);
- if (!placed || !cfg) return []; // nothing placed, or no config: SHD-R01 says so
- const t = targets(cfg, root);
- const out: string[] = [];
- const items = index();
- for (const n of placed.components) {
- const file = items[n]?.file;
- if (file && !existsSync(join(root, t.ui, file))) out.push(`${t.ui}/${file}: component ${n} is missing from the folder — restore it: shtuk run shadcn place`);
- }
- const inside = (rel: string) => rel === t.ui || rel.startsWith(`${t.ui}/`);
- for (const rel of projectFiles(['tsx', 'jsx', 'ts', 'js'])) {
- if (inside(rel)) continue;
- const text = read(rel, root);
- const radix = /from\s+["'](radix-ui|@radix-ui\/[\w-]+)["']/.exec(text);
- if (radix) out.push(`${rel}:${lineAt(text, radix.index)}: import of ${radix[1]} outside the components folder ${t.ui}/ — use the component from there or put a new one there`);
- const slot = /data-slot=["'][\w-]+["']/.exec(text);
- if (slot) out.push(`${rel}:${lineAt(text, slot.index)}: UI component (${slot[0]}) outside the folder ${t.ui}/ — move it there`);
- }
- return out;
- }
- if (import.meta.main) report(check());