86 lines
3.1 KiB
JavaScript
86 lines
3.1 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* 设计规则的可执行部分(design-system.md / code-standards.md / design-principles.md)。
|
|
*
|
|
* 四条检查,任一命中即退出码 1:
|
|
* 1. 组件里的颜色字面量(main.css 之外,白名单只有 MindMapViewer 的导图调色板)
|
|
* 2. transition: all
|
|
* 3. 模板里写死的颜色(:color="'#…'" 这类)
|
|
* 4. 界面旧名「残片」(现行名:知识碎片)
|
|
*
|
|
* 只在 src/ 下扫描;白名单的取值登记在 design-system.md 的「颜色白名单」一节。
|
|
*/
|
|
import { readdirSync, readFileSync, statSync } from 'node:fs';
|
|
import { join, relative } from 'node:path';
|
|
|
|
const ROOT = process.cwd();
|
|
const SRC = join(ROOT, 'src');
|
|
const HEX_ALLOWLIST = new Set(['components/MindMapViewer.vue']);
|
|
const SOURCE_EXT = new Set(['.vue', '.ts', '.css']);
|
|
const OLD_NAME = '残片';
|
|
|
|
const walk = (dir) => {
|
|
const files = [];
|
|
for (const entry of readdirSync(dir)) {
|
|
const full = join(dir, entry);
|
|
if (statSync(full).isDirectory()) files.push(...walk(full));
|
|
else files.push(full);
|
|
}
|
|
return files;
|
|
};
|
|
|
|
const files = walk(SRC).filter((file) => [...SOURCE_EXT].some((ext) => file.endsWith(ext)));
|
|
const violations = [];
|
|
const report = (file, line, rule, detail) => {
|
|
violations.push({ file: relative(ROOT, file), line, rule, detail });
|
|
};
|
|
|
|
for (const file of files) {
|
|
const rel = relative(SRC, file).split('\\').join('/');
|
|
const content = readFileSync(file, 'utf8');
|
|
const lines = content.split('\n');
|
|
const isVue = file.endsWith('.vue');
|
|
const isMainCss = rel === 'assets/main.css';
|
|
|
|
if (isVue) {
|
|
const templateColors = [
|
|
/:[\w-]+\s*=\s*"[^"]*#[0-9a-fA-F]{3,8}/g,
|
|
/:[\w-]+\s*=\s*'[^']*#[0-9a-fA-F]{3,8}/g,
|
|
];
|
|
for (const templateColor of templateColors) {
|
|
for (const match of content.matchAll(templateColor)) {
|
|
const line = content.slice(0, match.index).split('\n').length;
|
|
report(file, line, 'no-template-color', '模板绑定里写死颜色,改成按状态给 class');
|
|
}
|
|
}
|
|
}
|
|
|
|
lines.forEach((line, index) => {
|
|
const lineNumber = index + 1;
|
|
|
|
if (isVue && !HEX_ALLOWLIST.has(rel)) {
|
|
const hex = line.match(/#[0-9a-fA-F]{3,8}\b/);
|
|
if (hex) report(file, lineNumber, 'no-hex', `组件里写颜色字面量 ${hex[0]},改用 var(--x)`);
|
|
}
|
|
|
|
if (!isMainCss && /transition\s*:\s*all\b/i.test(line)) {
|
|
report(file, lineNumber, 'no-transition-all', 'transition: all 会把无关属性一起动画,显式列出属性');
|
|
}
|
|
|
|
if (line.includes(OLD_NAME)) {
|
|
report(file, lineNumber, 'no-old-name', `旧名「${OLD_NAME}」不得出现在代码与界面,现行名是「知识碎片」`);
|
|
}
|
|
});
|
|
}
|
|
|
|
if (violations.length) {
|
|
console.error(`ui-lint: ${violations.length} 处违反设计规则\n`);
|
|
for (const { file, line, rule, detail } of violations) {
|
|
console.error(` ${file}:${line} [${rule}] ${detail}`);
|
|
}
|
|
console.error('\n规则依据:.agents/roles/design-system.md、code-standards.md、design-principles.md');
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`ui-lint: ${files.length} 个文件通过(颜色字面量 / transition: all / 模板颜色 / 旧名)`);
|