包文件→ .shtukbase/packages/keer-ui/checks/library.ts
checks/library.ts
40 行, 2.0 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // KU-R01. The component library is whole in the project: placed, every file of the manifest on its place,
- // every relative import of the library (.ts/.tsx and CSS @import) resolves to a file.
- import { existsSync, readFileSync, statSync } from 'node:fs';
- import { dirname, join } from 'node:path';
- import { CONFIG, MANIFEST, config, files, readManifest } from '../actions/kitlib.ts';
- export const RULE = { id: 'ku-r01', title: 'The component library is intact' };
- const isFile = (p: string) => existsSync(p) && statSync(p).isFile();
- const CODE_EXT = ['', '.tsx', '.ts', '.jsx', '.js', '/index.tsx', '/index.ts'];
- export function check(root = process.cwd()): string[] {
- const { target } = config(root);
- const man = readManifest(root, target);
- if (!man) return [`${target}/${MANIFEST}: the library is not placed — run \`shtuk run keer-ui place\` (the folder is the target: field in /${CONFIG})`];
- const out: string[] = [];
- for (const rel of Object.keys(man.files).sort())
- if (!existsSync(join(root, target, rel))) out.push(`${target}/${rel}: library file is missing — restore it: \`shtuk run keer-ui place\``);
- for (const rel of files(join(root, target))) {
- const file = join(root, target, rel);
- if (!existsSync(file)) continue;
- const text = readFileSync(file, 'utf8');
- const here = dirname(file);
- if (/\.(tsx?|jsx?)$/.test(rel)) {
- for (const m of text.matchAll(/(?:from|import)\s*\(?\s*["'](\.{1,2}\/[^"']+)["']/g))
- if (!CODE_EXT.some((e) => isFile(join(here, m[1] + e))))
- out.push(`${target}/${rel}: import "${m[1]}" finds no file`);
- } else if (rel.endsWith('.css')) {
- for (const m of text.matchAll(/@import\s+(?:url\()?["'](\.{1,2}\/[^"']+)["']/g))
- if (!isFile(join(here, m[1]))) out.push(`${target}/${rel}: @import "${m[1]}" finds no file — check tokens: in /${CONFIG}`);
- }
- }
- return out;
- }
- if (import.meta.main) {
- const out = check();
- if (out.length) console.log(out.join('\n'));
- process.exitCode = out.length ? 1 : 0;
- }