archivo del paquete→ .shtukbase/packages/design-tokens/checks/contract.ts
checks/contract.ts
39 líneas, 2.0 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // DTK-R03. The built tokens of the project's style meet the token contract v1 (/design/contract.json): every required
- // name is set in `:root`, every colour also in the dark scheme. The CSS is `/design/<style>/tokens.css` of the style
- // chosen on /design/design.md, or a file given by hand: `shtuk run design-tokens contract --css <file>`.
- // No style chosen: nothing to judge. Optional extension names that the style leaves out are named in a `note:` line
- // (the defaults of the contract apply); a note is not a finding.
- import { existsSync, readFileSync } from 'node:fs';
- import { join } from 'node:path';
- import { checkCss, fallbackLine, fallbacks, readContract, styleCssPath } from '../lib/contract.ts';
- export const RULE = { id: 'dtk-r03', title: 'The style meets the token contract' };
- export function check(root = process.cwd(), css: string | null = null): { findings: string[]; notes: string[] } {
- const rel = css ?? styleCssPath(root);
- if (!rel) return { findings: [], notes: [] };
- const p = join(root, rel);
- if (!existsSync(p)) {
- 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: [] };
- }
- const text = readFileSync(p, 'utf8');
- const contract = readContract(root);
- const findings = checkCss(text, rel, contract);
- const missing = fallbacks(text, contract);
- const notes = missing.length ? [`note: ${rel} leaves out optional names; the defaults of /design/contract.json apply to: ${fallbackLine(missing)}`] : [];
- return { findings, notes };
- }
- if (import.meta.main) {
- const args = process.argv.slice(2);
- const i = args.indexOf('--css');
- const css = i >= 0 ? args[i + 1] ?? '' : null;
- if (i >= 0 && !css) {
- console.log('--css needs a file: shtuk run design-tokens contract --css <file>');
- process.exitCode = 1;
- } else {
- const { findings, notes } = check(process.cwd(), css);
- if (findings.length || notes.length) console.log([...findings, ...notes].join('\n'));
- process.exitCode = findings.length ? 1 : 0;
- }
- }