包文件→ .shtukbase/packages/design-tokens/checks/health.ts
checks/health.ts
50 行, 2.8 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // DTK-R04. The health of a design style, a norm in the mode `warn` (convention /design/conventions/style.md): it reports
- // how well the style meets the token contract in bands and never fails. One line per band for each style judged:
- // health style/<id>: full | missing: <names> | fallbacks: <names>, and extra: <names> besides any of them
- // The style judged is the one chosen on /design/design.md; `--style <id>` names another (/design/<id>/tokens.css), `--all`
- // takes every /design/<id>/tokens.css, `--css <file>` any CSS file by hand. Exit code 0 always.
- import { existsSync, readFileSync, readdirSync } from 'node:fs';
- import { join } from 'node:path';
- import { fallbackLine, health, readContract, styleChoice } from '../lib/contract.ts';
- export const RULE = { id: 'dtk-r04', title: 'The health of a style against the token contract' };
- const SHOWN = 12;
- const names = (n: string[]) => (n.length > SHOWN ? `${n.slice(0, SHOWN).join(', ')} (+${n.length - SHOWN} more)` : n.join(', '));
- /** The lines for one stylesheet; `subject` is what the lines name (the style id or a file). */
- export function lines(css: string, subject: string, root = process.cwd()): string[] {
- const h = health(css, readContract(root));
- const at = `health style/${subject}:`;
- const out: string[] = [];
- if (h.missing.length) out.push(`${at} missing: ${names(h.missing)}`);
- if (h.fallbacks.length) out.push(`${at} fallbacks: ${fallbackLine(h.fallbacks)}`);
- if (!out.length) out.push(`${at} full`);
- if (h.extra.length) out.push(`${at} extra: ${names(h.extra)}`);
- return out;
- }
- /** Every `design/<id>/tokens.css` of the project: [id, path]. */
- function allStyles(root: string): [string, string][] {
- const dir = join(root, 'design');
- if (!existsSync(dir)) return [];
- return readdirSync(dir).sort().filter((n) => existsSync(join(dir, n, 'tokens.css'))).map((n) => [n, `design/${n}/tokens.css`]);
- }
- export function check(root = process.cwd(), opts: { style?: string; css?: string; all?: boolean } = {}): string[] {
- const chosen = opts.style || styleChoice(root).style;
- const todo: [string, string][] = opts.css ? [[opts.css, opts.css]] : opts.all ? allStyles(root) : chosen ? [[chosen, `design/${chosen}/tokens.css`]] : [];
- const out: string[] = [];
- for (const [subject, rel] of todo) {
- if (!existsSync(join(root, rel))) out.push(`health style/${subject}: missing: ${rel} (the style's tokens are not built)`);
- else out.push(...lines(readFileSync(join(root, rel), 'utf8'), subject, root));
- }
- return out;
- }
- if (import.meta.main) {
- const args = process.argv.slice(2);
- const opt = (name: string) => { const i = args.indexOf(name); return i >= 0 ? args[i + 1] : undefined; };
- const out = check(process.cwd(), { style: opt('--style'), css: opt('--css'), all: args.includes('--all') });
- if (out.length) console.log(out.join('\n'));
- }