Files
ClassHelper/scripts/check-design-tokens.mjs
T
LoveChengke 4449fbd0a1
发布官网与文档站 / build (push) Waiting to run
feat(desktop): 完善灵动岛设计令牌与外观配置
2026-11-17 18:57:32 +08:00

137 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
const root = process.cwd();
const tokenFile = path.join(root, 'packages/ui/src/tokens.css');
const islandRoot = path.join(root, 'packages/desktop-client/src/island');
const scopedFiles = [
islandRoot,
path.join(root, 'packages/desktop-client/src/main/island.ts'),
path.join(root, 'packages/desktop-client/src/main/config.ts'),
path.join(root, 'packages/desktop-client/src/preload/island.ts'),
path.join(root, 'packages/desktop-client/src/renderer/components/IslandComponentSettings.vue'),
path.join(root, 'packages/desktop-client/src/renderer/styles/island-editor.css'),
path.join(root, 'packages/desktop-client/src/renderer/views/settings/IslandEditorView.vue'),
path.join(root, 'packages/desktop-client/src/renderer/views/settings/IslandSettingsView.vue'),
];
const source = fs.readFileSync(tokenFile, 'utf8');
const rootBlock = source.match(/:root\s*\{([\s\S]*?)\n\}/)?.[1] ?? '';
const darkBlock = source.match(/html\.dark\s*\{([\s\S]*?)\n\}/)?.[1] ?? '';
function tokenNames(block) {
return [...block.matchAll(/(--ci-[a-z0-9-]+)\s*:/g)].map((match) => match[1]);
}
const light = new Set(tokenNames(rootBlock));
const dark = new Set(tokenNames(darkBlock));
const errors = [];
if (!light.size || !dark.size) errors.push('无法解析 :root 或 html.dark 的 --ci-* token');
for (const name of light) if (!dark.has(name)) errors.push(`深色主题缺少 ${name}`);
for (const name of dark) if (!light.has(name)) errors.push(`亮色主题缺少 ${name}`);
const required = [
'--ci-accent',
'--ci-accent-2',
'--ci-accent-fallback',
'--ci-accent-palette-fallback',
'--ci-accent-fill',
'--ci-accent-fill-tertiary',
'--ci-accent-text',
'--ci-text-on-accent',
'--ci-accent-soft',
'--ci-text-1',
'--ci-text-2',
'--ci-text-3',
'--ci-text-4',
'--ci-text-5',
'--ci-bg-base',
'--ci-bg-secondary',
'--ci-bg-tertiary',
'--ci-island-bg-custom',
'--ci-island-opacity',
'--ci-island-bg-opacity',
'--ci-line-opacity',
'--ci-stroke-island',
'--ci-stroke-control',
'--ci-stroke-divider',
'--ci-stroke-flyout',
'--ci-stroke-card',
'--ci-stroke-drag',
'--ci-stroke-tutorial',
'--ci-shadow-island',
'--ci-shadow-mask',
'--ci-shadow-drawer',
'--ci-shadow-drawer-x',
'--ci-shadow-classic-mask',
'--ci-shadow-adorn',
'--ci-font',
'--ci-font-island',
'--ci-font-backend',
'--ci-font-icon-fluent',
'--ci-font-icon-lucide',
'--ci-font-icon-sf',
'--ci-fs-secondary',
'--ci-fs-body',
'--ci-fs-emphasized',
'--ci-fs-large',
'--ci-radius-island',
'--ci-radius-island-local',
'--ci-radius-popup',
'--ci-radius-card',
'--ci-radius-control',
'--ci-radius-drawer',
'--ci-radius-pill',
'--ci-island-height',
'--ci-island-pad-x',
'--ci-island-min-w',
'--ci-component-gap',
'--ci-line-gap',
'--ci-progress-height',
'--ci-z-window',
'--ci-z-window-top',
'--ci-z-mask',
'--ci-z-drag',
'--ci-z-timeline-adorn',
'--ci-z-sentinel',
'--ci-dpi-basis',
'--ci-scale',
'--ci-vertical-safe-area',
];
for (const name of required) if (!light.has(name)) errors.push(`缺少基础 token ${name}`);
function files(directory) {
return fs.readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
const target = path.join(directory, entry.name);
return entry.isDirectory() ? files(target) : [target];
});
}
const scoped = scopedFiles.flatMap((file) => (fs.statSync(file).isDirectory() ? files(file) : [file]));
for (const file of scoped.filter((value) => /\.(css|vue|ts)$/.test(value))) {
const text = fs.readFileSync(file, 'utf8');
const relative = path.relative(root, file);
if (/#[0-9a-f]{3,8}\b/i.test(text)) errors.push(`${relative} 含裸颜色字面量`);
if (/(?<![\w-])rgba?\(/i.test(text)) errors.push(`${relative} 含裸 rgb/rgba 色值`);
for (const match of text.matchAll(/--ci-([A-Za-z0-9_-]+)/g)) {
if (/[A-Z_]/.test(match[1])) errors.push(`${relative} 的 token 名不是 kebab-case:--ci-${match[1]}`);
}
}
for (const file of files(path.join(root, 'packages/desktop-client/src')).filter((value) =>
value.endsWith('.css'),
)) {
const text = fs.readFileSync(file, 'utf8');
if (/^\s*--ci-[a-z0-9-]+\s*:/m.test(text)) {
errors.push(`${path.relative(root, file)} 在 tokens.css 之外定义了 --ci-*`);
}
}
if (errors.length) {
console.error(`设计 token 检查失败(${errors.length} 项):`);
for (const error of errors) console.error(`- ${error}`);
process.exitCode = 1;
} else {
console.log(`设计 token 检查通过:亮/暗各 ${light.size} 个 --ci-*,岛相关文件无裸颜色。`);
}