Files
openmotion/scripts/design-check.ts
Tom Huang 25490a6c1c Polish MCP onboarding, template previews, and sharing metadata (#402)
* Polish MCP onboarding, template previews, and sharing metadata

Start Agent setup from public installation guidance, reuse template preview cards, simplify MCP copy, and refresh social metadata with the README hero.

* Reuse setupGuideUrl in templatePrompt

templatePrompt resolved the published install-guide URL inline, so the
published-artifact path added in this branch had two sources that could
drift. Delegate to setupGuideUrl and cover the template prompt for the
default and dev catalog environments.

Generated-By: looper 0.16.2 (runner=fixer, agent=pi)

---------

Co-authored-by: looper <luogao@users.noreply.github.com>
2026-11-15 19:19:58 +08:00

66 lines
5.5 KiB
TypeScript

import assert from 'node:assert/strict';
import { readFile, readdir } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import { execFileSync } from 'node:child_process';
import { designTokens } from './design-tokens.ts';
import { providerIcons, providerIconFiles } from '../packages/design-system/provider-icons.js';
import { publicDesignFiles, designSystemRuntimeFiles } from '../packages/design-system/index.js';
const root = new URL('../', import.meta.url);
const luminance = (hex: string) => {
const values = hex.slice(1).match(/../g)!.map(value => Number.parseInt(value, 16) / 255)
.map(value => value <= .04045 ? value / 12.92 : ((value + .055) / 1.055) ** 2.4);
return values[0] * .2126 + values[1] * .7152 + values[2] * .0722;
};
export async function checkDesign() {
const tokens = await designTokens(true);
const presets = JSON.parse(await readFile(new URL('shared/provider-presets.json', root), 'utf8')) as { id: string }[];
for (const id of [...presets.map(provider => provider.id), 'account', 'opendesign', 'codex', 'fal', 'atlascloud', 'wavespeed']) {
assert.ok(Object.hasOwn(providerIcons, id), `Built-in provider ${id} needs an explicit brand or generic icon.`);
}
for (const file of providerIconFiles) {
assert.ok(publicDesignFiles.includes(file) && designSystemRuntimeFiles.includes(file), `${file} must be served and distributed.`);
const svg = await readFile(new URL('packages/design-system/' + file, root), 'utf8');
assert.match(svg, /^<svg\b/);
assert.doesNotMatch(svg, /<script|<foreignObject|\son[a-z]+\s*=|(?:href|xlink:href)=["'](?:https?:|\/\/)/i, `${file} must remain self-contained static artwork.`);
}
for (const license of ['providers/lobe-icons-LICENSE.txt', 'providers/cherry-ui-LICENSE.txt']) {
assert.ok(designSystemRuntimeFiles.includes(license));
assert.match(await readFile(new URL('packages/design-system/' + license, root), 'utf8'), /MIT License/);
}
assert.deepEqual(Object.keys(tokens.themes.light).sort(), Object.keys(tokens.themes.dark).sort(), 'Both themes need the same semantic colors.');
const coreAliases = { 'color-surface': 'panel', 'color-text-body': 'text', 'font-sans': 'font-sans', 'radius-button': 'radius-sm', 'color-template-controls-bg': 'gallery-control', 'color-template-text': 'strong' };
for (const [alias, role] of Object.entries(coreAliases)) assert.equal(tokens.studio[alias], `var(--motion-${role})`, `${alias} must inherit the shared theme`);
const material = await readFile(new URL('packages/design-system/material.css', root), 'utf8');
const materialNames = [...material.matchAll(/(--motion-[\w-]+)\s*:/g)].map(match => match[1]);
const names = new Set([...Object.keys(tokens.shared), ...Object.keys(tokens.themes.light)].map(name => '--motion-' + name).concat(materialNames));
const consumerFiles = (await Promise.all(['studio/src/', 'web/'].map(async directory =>
(await readdir(new URL(directory, root), { recursive: true }))
.filter(file => file.endsWith('.css')).map(file => directory + file)
))).flat();
const sharedNames = new Set([...names, ...Object.keys(tokens.studio).map(name => '--' + name)]);
for (const file of [...consumerFiles, 'packages/design-system/base.css', 'packages/design-system/material.css', 'packages/design-system/controls.css', 'packages/design-system/catalog.css', 'packages/design-system/loading.css', 'packages/design-system/studio/application.css', 'packages/design-system/studio/adapter.css', 'packages/design-system/studio/control-states.css', 'packages/design-system/studio/feedback.css', 'packages/design-system/studio/mcp.css', 'packages/design-system/studio/template-cards.css']) {
const css = await readFile(new URL(file, root), 'utf8');
assert.doesNotMatch(css, /#[\da-f]{3,8}\b|\brgba?\(|\bhsla?\(/i, `${file}: UI colors belong in tokens.json.`);
for (const match of css.matchAll(/var\((--motion-[\w-]+)/g)) assert.ok(names.has(match[1]), `${file}: undefined ${match[1]}`);
if (consumerFiles.includes(file)) {
for (const match of css.matchAll(/(--[\w-]+)\s*:/g)) {
assert.ok(!sharedNames.has(match[1]), `${file}: shared role ${match[1]} must be defined in packages/design-system, not overridden by a feature.`);
}
}
}
for (const [name, value] of Object.entries(tokens.studio) as [string, string][]) {
for (const match of value.matchAll(/var\((--motion-[\w-]+)/g)) assert.ok(names.has(match[1]), `Studio token ${name}: undefined ${match[1]}`);
}
for (const [theme, values] of Object.entries(tokens.themes) as [string, Record<string, string>][]) {
for (const [ink, surface] of [['text', 'shell'], ['text', 'workspace'], ['text', 'panel'], ['text', 'inset'], ['muted', 'panel'], ['dim', 'panel'], ['dim', 'bg'], ['accent', 'accent-soft'], ['on-action', 'action'], ['danger', 'danger-soft'], ['warning', 'warning-soft'], ['strong', 'highlight']]) {
const a = luminance(values[ink]), b = luminance(values[surface]);
assert.ok((Math.max(a, b) + .05) / (Math.min(a, b) + .05) >= 4.5, `${theme}: ${ink} on ${surface} needs 4.5:1 contrast.`);
}
}
for (const file of ['theme.js', 'catalog.js', 'catalog-view.js', 'token-model.js', 'generate.js']) execFileSync(process.execPath, ['--check', fileURLToPath(new URL('packages/design-system/' + file, root))]);
console.log('Design token freshness, theme parity, UI color references, contrast and browser scripts passed.');
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) await checkDesign();