- scripts/check-layout.cjs:rem 两位小数、字号 10 档、圆角 3 档、5px 间距网格、高频硬编码颜色 全量校验 - package.json 增加 lint:layout 脚本;devDependencies 增加 qrcode-generator@2.0.4(本地渲染二维码,dependencies 保持为空)
97 lines
4.2 KiB
JavaScript
97 lines
4.2 KiB
JavaScript
/**
|
||
* UI 排版规范自检(对应 README「UI 排版规范」)
|
||
*
|
||
* node scripts/check-layout.cjs # 或 npm run lint:layout
|
||
*
|
||
* 可机械检查的项:
|
||
* 1. rem 值最多 2 位小数
|
||
* 2. font-size 只能取 10 档刻度
|
||
* 3. border-radius 只能取 3 档(或 50% / px)
|
||
* 4. padding / margin / gap 取 0.05rem 的倍数;小于 0.05rem 的微内边距取 0.02rem 的倍数
|
||
* 5. 高频色值不能再写死(要写 var(--wms-*))
|
||
* 例外面板:src/components/scan-diag-panel.vue(整块 px 的诊断面板)与退件页摄像头浮窗(px),已在 README 说明。
|
||
* 退出码:发现不合规为 1,可直接用于 CI / 提交前钩子。
|
||
*/
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const SKIP_FILES = ['src/components/scan-diag-panel.vue']
|
||
const FONT_TIERS = [0.15, 0.17, 0.18, 0.2, 0.21, 0.22, 0.25, 0.3, 0.35, 0.45]
|
||
const RADIUS_TIERS = [0.03, 0.07, 0.13]
|
||
const SPACE_PROPS = /^(row-gap|column-gap|gap|padding|margin)(-(top|right|bottom|left))?$/
|
||
// 高频色 → 必须用 token(与 base.css 的 :root 对应)
|
||
const COLOR_TOKENS = {
|
||
'#2f7dff': '--wms-primary', '#409eff': '--wms-primary-alt',
|
||
'#67c23a': '--wms-success', '#2fbf71': '--wms-success-alt',
|
||
'#ff7d00': '--wms-warning', '#f56c6c': '--wms-danger', '#e64340': '--wms-danger-strong',
|
||
'#333': '--wms-text', '#333333': '--wms-text', '#303133': '--wms-text-strong',
|
||
'#666': '--wms-text-secondary', '#666666': '--wms-text-secondary',
|
||
'#606266': '--wms-text-tertiary', '#909399': '--wms-text-muted',
|
||
'#999': '--wms-text-placeholder', '#999999': '--wms-text-placeholder',
|
||
'#c0c4cc': '--wms-text-disabled', '#fff': '--wms-card-bg / --wms-white', '#ffffff': '--wms-card-bg / --wms-white',
|
||
'#f8f8f8': '--wms-page-bg', '#f5f7fa': '--wms-bg-subtle', '#eef5ff': '--wms-bg-tint',
|
||
'#dcdfe6': '--wms-border', '#d9dde8': '--wms-border-alt',
|
||
'#ebeef5': '--wms-border-light', '#d0d5e0': '--wms-border-strong'
|
||
}
|
||
|
||
function walk (dir, out = []) {
|
||
for (const e of fs.readdirSync(path.join(root, dir), { withFileTypes: true })) {
|
||
const rel = dir + '/' + e.name
|
||
if (e.isDirectory()) walk(rel, out)
|
||
else if (/\.(vue|css)$/.test(e.name)) out.push(rel)
|
||
}
|
||
return out
|
||
}
|
||
|
||
const isMultiple = (v, step) => Math.abs(v / step - Math.round(v / step)) < 1e-6
|
||
const problems = []
|
||
|
||
for (const rel of walk('src')) {
|
||
if (SKIP_FILES.includes(rel)) continue
|
||
const src = fs.readFileSync(path.join(root, rel), 'utf8')
|
||
const m = src.match(/<style[^>]*>([\s\S]*?)<\/style>/)
|
||
const css = (m ? m[1] : rel.endsWith('.css') ? src : '')
|
||
.replace(/\/\*[\s\S]*?\*\//g, '')
|
||
.replace(/:root\s*\{[\s\S]*?\n\}/, '') // token 定义块不算使用
|
||
if (!css) continue
|
||
|
||
// 1. 小数位
|
||
for (const mm of css.matchAll(/([\d.]+)rem/g)) {
|
||
const dec = (mm[1].split('.')[1] || '').length
|
||
if (dec > 2) problems.push(`${rel}: rem 小数位超过 2 位 → ${mm[0]}`)
|
||
}
|
||
// 2. 字号
|
||
for (const mm of css.matchAll(/font-size:\s*([\d.]+)rem/g)) {
|
||
if (!FONT_TIERS.includes(Number(mm[1]))) problems.push(`${rel}: 字号不在 10 档内 → ${mm[0]}`)
|
||
}
|
||
// 3. 圆角
|
||
for (const mm of css.matchAll(/border-radius:\s*([^;{}]+)/g)) {
|
||
for (const v of mm[1].matchAll(/([\d.]+)rem/g)) {
|
||
if (!RADIUS_TIERS.includes(Number(v[1]))) problems.push(`${rel}: 圆角不在 3 档内 → border-radius: ${mm[1].trim()}`)
|
||
}
|
||
}
|
||
// 4. 间距
|
||
for (const mm of css.matchAll(/([-\w]+)\s*:\s*([^;{}]+)/g)) {
|
||
if (!SPACE_PROPS.test(mm[1])) continue
|
||
for (const v of mm[2].matchAll(/([\d.]+)rem/g)) {
|
||
const val = Number(v[1])
|
||
const ok = val >= 0.05 ? isMultiple(val, 0.05) : isMultiple(val, 0.02)
|
||
if (!ok) problems.push(`${rel}: 间距不在网格上 → ${mm[1]}: ${mm[2].trim()}`)
|
||
}
|
||
}
|
||
// 5. 高频色写死
|
||
for (const mm of css.matchAll(/#[0-9a-fA-F]{3,8}\b/g)) {
|
||
const token = COLOR_TOKENS[mm[0].toLowerCase()]
|
||
if (token) problems.push(`${rel}: 高频色应写 var(${token}) → ${mm[0]}`)
|
||
}
|
||
}
|
||
|
||
if (problems.length === 0) {
|
||
console.log('✅ 排版规范自检通过(字号 10 档 / 圆角 3 档 / 间距 5px 网格 / rem ≤2 位小数 / 颜色用 token)')
|
||
} else {
|
||
console.log('❌ 发现 ' + problems.length + ' 处不合规:')
|
||
for (const p of problems) console.log(' ' + p)
|
||
process.exitCode = 1
|
||
}
|