ShtukbaseStandard
包文件→ .shtukbase/packages/design-tokens/checks/health.ts

checks/health.ts

50 行, 2.8 KB.

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

  1. // DTK-R04. The health of a design style, a norm in the mode `warn` (convention /design/conventions/style.md): it reports
  2. // how well the style meets the token contract in bands and never fails. One line per band for each style judged:
  3. // health style/<id>: full | missing: <names> | fallbacks: <names>, and extra: <names> besides any of them
  4. // The style judged is the one chosen on /design/design.md; `--style <id>` names another (/design/<id>/tokens.css), `--all`
  5. // takes every /design/<id>/tokens.css, `--css <file>` any CSS file by hand. Exit code 0 always.
  6. import { existsSync, readFileSync, readdirSync } from 'node:fs';
  7. import { join } from 'node:path';
  8. import { fallbackLine, health, readContract, styleChoice } from '../lib/contract.ts';
  9. export const RULE = { id: 'dtk-r04', title: 'The health of a style against the token contract' };
  10. const SHOWN = 12;
  11. const names = (n: string[]) => (n.length > SHOWN ? `${n.slice(0, SHOWN).join(', ')} (+${n.length - SHOWN} more)` : n.join(', '));
  12. /** The lines for one stylesheet; `subject` is what the lines name (the style id or a file). */
  13. export function lines(css: string, subject: string, root = process.cwd()): string[] {
  14. const h = health(css, readContract(root));
  15. const at = `health style/${subject}:`;
  16. const out: string[] = [];
  17. if (h.missing.length) out.push(`${at} missing: ${names(h.missing)}`);
  18. if (h.fallbacks.length) out.push(`${at} fallbacks: ${fallbackLine(h.fallbacks)}`);
  19. if (!out.length) out.push(`${at} full`);
  20. if (h.extra.length) out.push(`${at} extra: ${names(h.extra)}`);
  21. return out;
  22. }
  23. /** Every `design/<id>/tokens.css` of the project: [id, path]. */
  24. function allStyles(root: string): [string, string][] {
  25. const dir = join(root, 'design');
  26. if (!existsSync(dir)) return [];
  27. return readdirSync(dir).sort().filter((n) => existsSync(join(dir, n, 'tokens.css'))).map((n) => [n, `design/${n}/tokens.css`]);
  28. }
  29. export function check(root = process.cwd(), opts: { style?: string; css?: string; all?: boolean } = {}): string[] {
  30. const chosen = opts.style || styleChoice(root).style;
  31. const todo: [string, string][] = opts.css ? [[opts.css, opts.css]] : opts.all ? allStyles(root) : chosen ? [[chosen, `design/${chosen}/tokens.css`]] : [];
  32. const out: string[] = [];
  33. for (const [subject, rel] of todo) {
  34. if (!existsSync(join(root, rel))) out.push(`health style/${subject}: missing: ${rel} (the style's tokens are not built)`);
  35. else out.push(...lines(readFileSync(join(root, rel), 'utf8'), subject, root));
  36. }
  37. return out;
  38. }
  39. if (import.meta.main) {
  40. const args = process.argv.slice(2);
  41. const opt = (name: string) => { const i = args.indexOf(name); return i >= 0 ? args[i + 1] : undefined; };
  42. const out = check(process.cwd(), { style: opt('--style'), css: opt('--css'), all: args.includes('--all') });
  43. if (out.length) console.log(out.join('\n'));
  44. }