ShtukbaseStandard
файл пакета→ .shtukbase/packages/design-tokens/checks/contract.ts

checks/contract.ts

39 строк, 2.0 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // DTK-R03. The built tokens of the project's style meet the token contract v1 (/design/contract.json): every required
  2. // name is set in `:root`, every colour also in the dark scheme. The CSS is `/design/<style>/tokens.css` of the style
  3. // chosen on /design/design.md, or a file given by hand: `shtuk run design-tokens contract --css <file>`.
  4. // No style chosen: nothing to judge. Optional extension names that the style leaves out are named in a `note:` line
  5. // (the defaults of the contract apply); a note is not a finding.
  6. import { existsSync, readFileSync } from 'node:fs';
  7. import { join } from 'node:path';
  8. import { checkCss, fallbackLine, fallbacks, readContract, styleCssPath } from '../lib/contract.ts';
  9. export const RULE = { id: 'dtk-r03', title: 'The style meets the token contract' };
  10. export function check(root = process.cwd(), css: string | null = null): { findings: string[]; notes: string[] } {
  11. const rel = css ?? styleCssPath(root);
  12. if (!rel) return { findings: [], notes: [] };
  13. const p = join(root, rel);
  14. if (!existsSync(p)) {
  15. return { findings: [`${rel}: the file is missing — build the style's tokens (shtuk run design-tokens build, or the build of the style package)`], notes: [] };
  16. }
  17. const text = readFileSync(p, 'utf8');
  18. const contract = readContract(root);
  19. const findings = checkCss(text, rel, contract);
  20. const missing = fallbacks(text, contract);
  21. const notes = missing.length ? [`note: ${rel} leaves out optional names; the defaults of /design/contract.json apply to: ${fallbackLine(missing)}`] : [];
  22. return { findings, notes };
  23. }
  24. if (import.meta.main) {
  25. const args = process.argv.slice(2);
  26. const i = args.indexOf('--css');
  27. const css = i >= 0 ? args[i + 1] ?? '' : null;
  28. if (i >= 0 && !css) {
  29. console.log('--css needs a file: shtuk run design-tokens contract --css <file>');
  30. process.exitCode = 1;
  31. } else {
  32. const { findings, notes } = check(process.cwd(), css);
  33. if (findings.length || notes.length) console.log([...findings, ...notes].join('\n'));
  34. process.exitCode = findings.length ? 1 : 0;
  35. }
  36. }