2026-10-08 16:48:05 +08:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
/* ==========================================================================
|
|
|
|
|
|
样式一致性核对
|
|
|
|
|
|
① 以阶段 0 / 1 的既有页面为基线,收集它们用到的全部 class;
|
|
|
|
|
|
② 扫描新模块页面,报告任何「基线里没有、doc.css 里也没有」的 class。
|
|
|
|
|
|
这能证明新页面的样式与既有页面完全同源,而不是自创了一套。
|
|
|
|
|
|
node .sre-s23/verify-template.js <模块目录名...>
|
|
|
|
|
|
========================================================================== */
|
|
|
|
|
|
const fs = require("fs");
|
|
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
2026-10-15 11:55:58 +08:00
|
|
|
|
const SITE = path.resolve(__dirname, "..", "SRE工程师", "sre-learning-path");
|
2026-10-08 16:48:05 +08:00
|
|
|
|
const MODDIR = path.join(SITE, "modules");
|
|
|
|
|
|
const css = fs.readFileSync(path.join(SITE, "assets", "doc.css"), "utf8");
|
|
|
|
|
|
const cssClasses = new Set([...css.matchAll(/\.([a-zA-Z][a-zA-Z0-9_-]*)/g)].map(m => m[1]));
|
|
|
|
|
|
|
|
|
|
|
|
/* 只在复合选择器里出现、单独扫描时会误报的名字 */
|
|
|
|
|
|
const KNOWN = new Set(["prev", "current"]);
|
|
|
|
|
|
|
|
|
|
|
|
const classesOf = dir => {
|
|
|
|
|
|
const set = new Set();
|
|
|
|
|
|
for (const f of fs.readdirSync(path.join(MODDIR, dir))) {
|
|
|
|
|
|
if (!f.endsWith(".html")) continue;
|
|
|
|
|
|
const html = fs.readFileSync(path.join(MODDIR, dir, f), "utf8");
|
|
|
|
|
|
for (const m of html.matchAll(/class="([^"]+)"/g)) {
|
|
|
|
|
|
for (const t of m[1].split(/\s+/)) if (t) set.add(t);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return set;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const all = fs.readdirSync(MODDIR, { withFileTypes: true })
|
|
|
|
|
|
.filter(d => d.isDirectory()).map(d => d.name).sort();
|
|
|
|
|
|
const baseline = new Set();
|
|
|
|
|
|
for (const d of all.filter(x => /^s[01]-/.test(x))) {
|
|
|
|
|
|
for (const t of classesOf(d)) baseline.add(t);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const targets = process.argv.slice(2).length
|
|
|
|
|
|
? process.argv.slice(2)
|
|
|
|
|
|
: all.filter(x => /^s[23]-/.test(x));
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`基线:阶段 0/1 共 ${baseline.size} 种 class(来自 ${all.filter(x => /^s[01]-/.test(x)).length} 个模块)\n`);
|
|
|
|
|
|
let bad = 0;
|
|
|
|
|
|
for (const d of targets) {
|
|
|
|
|
|
const set = classesOf(d);
|
|
|
|
|
|
const alien = [...set].filter(t => !baseline.has(t) && !cssClasses.has(t) && !KNOWN.has(t));
|
|
|
|
|
|
const extra = [...set].filter(t => !baseline.has(t) && cssClasses.has(t));
|
|
|
|
|
|
if (alien.length) bad++;
|
|
|
|
|
|
console.log(`${alien.length ? "✗" : "✓"} ${d}:${set.size} 种 class` +
|
|
|
|
|
|
(alien.length ? ` —— 未定义样式:${alien.join(", ")}` : "") +
|
|
|
|
|
|
(extra.length ? `(doc.css 中存在但既有页面未用过:${extra.join(", ")})` : ""));
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log(bad ? `\n有 ${bad} 个模块使用了自创样式` : "\nOK 新页面只使用既有样式");
|
|
|
|
|
|
process.exit(bad ? 1 : 0);
|