ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/keer-ui/checks/library.ts

checks/library.ts

40 líneas, 2.0 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // KU-R01. The component library is whole in the project: placed, every file of the manifest on its place,
  2. // every relative import of the library (.ts/.tsx and CSS @import) resolves to a file.
  3. import { existsSync, readFileSync, statSync } from 'node:fs';
  4. import { dirname, join } from 'node:path';
  5. import { CONFIG, MANIFEST, config, files, readManifest } from '../actions/kitlib.ts';
  6. export const RULE = { id: 'ku-r01', title: 'The component library is intact' };
  7. const isFile = (p: string) => existsSync(p) && statSync(p).isFile();
  8. const CODE_EXT = ['', '.tsx', '.ts', '.jsx', '.js', '/index.tsx', '/index.ts'];
  9. export function check(root = process.cwd()): string[] {
  10. const { target } = config(root);
  11. const man = readManifest(root, target);
  12. if (!man) return [`${target}/${MANIFEST}: the library is not placed — run \`shtuk run keer-ui place\` (the folder is the target: field in /${CONFIG})`];
  13. const out: string[] = [];
  14. for (const rel of Object.keys(man.files).sort())
  15. if (!existsSync(join(root, target, rel))) out.push(`${target}/${rel}: library file is missing — restore it: \`shtuk run keer-ui place\``);
  16. for (const rel of files(join(root, target))) {
  17. const file = join(root, target, rel);
  18. if (!existsSync(file)) continue;
  19. const text = readFileSync(file, 'utf8');
  20. const here = dirname(file);
  21. if (/\.(tsx?|jsx?)$/.test(rel)) {
  22. for (const m of text.matchAll(/(?:from|import)\s*\(?\s*["'](\.{1,2}\/[^"']+)["']/g))
  23. if (!CODE_EXT.some((e) => isFile(join(here, m[1] + e))))
  24. out.push(`${target}/${rel}: import "${m[1]}" finds no file`);
  25. } else if (rel.endsWith('.css')) {
  26. for (const m of text.matchAll(/@import\s+(?:url\()?["'](\.{1,2}\/[^"']+)["']/g))
  27. if (!isFile(join(here, m[1]))) out.push(`${target}/${rel}: @import "${m[1]}" finds no file — check tokens: in /${CONFIG}`);
  28. }
  29. }
  30. return out;
  31. }
  32. if (import.meta.main) {
  33. const out = check();
  34. if (out.length) console.log(out.join('\n'));
  35. process.exitCode = out.length ? 1 : 0;
  36. }