Files
resume/scripts/check.mjs
T

107 lines
4.5 KiB
JavaScript
Raw Normal View History

// scripts/check.mjs —— 构建自检:必填字段、跨层同键、占位符残留、产物完整性
import fs from 'node:fs';
import path from 'node:path';
import { ROOT, DIST, loadAll, buildView, SECTION_REGISTRY, keyPaths, isObj } from './lib.mjs';
const errors = [];
const warnings = [];
const err = (m) => errors.push(m);
const warn = (m) => warnings.push(m);
const data = loadAll();
const views = [];
// 1. 视图构建 + 必填字段
for (const v of data.variants) {
const label = `${v.company}-${v.role}`;
let view;
try {
view = buildView(v, data);
} catch (e) {
err(`[${label}] 视图构建失败:${e.message}`);
continue;
}
views.push(view);
const m = view.merged;
for (const [key, val] of [
['name', m.name],
['contact.phone', m.contact && m.contact.phone],
['contact.email', m.contact && m.contact.email],
['education.school', m.education && m.education.school],
['education.major', m.education && m.education.major],
['site.baseUrl', m.site && m.site.baseUrl],
]) {
if (!val) err(`[${label}] 缺少必填字段 ${key}`);
}
if (!v.sections || !v.sections.length) err(`[${label}] 未声明任何板块`);
if (!v.meta || !v.meta.title) warn(`[${label}] 未设置 meta.title,将使用默认标题`);
if (!v.meta || !v.meta.description) warn(`[${label}] 未设置 meta.description`);
// 板块内引用的经历 id 必须存在
const expIds = new Set(Object.keys(m.experiences || {}));
for (const [orderKey, ids] of Object.entries(m.order || {})) {
for (const id of ids) if (!expIds.has(id)) err(`[${label}] order.${orderKey} 引用了不存在的经历 "${id}"`);
}
for (const [id, exp] of Object.entries(m.experiences || {})) {
if (exp && exp.bullets && !exp.bullets.length) warn(`[${label}] ${id} 的 bullets 为空`);
if (exp && exp.bullets === undefined && (exp.kind === 'internship' || exp.kind === 'project')) {
warn(`[${label}] ${id} 缺少 bullets(是否遗漏岗位维度内容?)`);
}
}
}
// 2. 跨层同键检查(同一字段改了一层、被另一层覆盖的风险)
// 仅对比「内容层」:companies/<x>.yaml 与 roles/<y>.yaml 的键路径交集
const META_KEYS = new Set(['navLabel', 'theme']);
for (const [cname, company] of Object.entries(data.companies)) {
for (const [rname, role] of Object.entries(data.roles)) {
const a = keyPaths(company);
const b = keyPaths(role);
for (const p of a) {
if (b.has(p) && !META_KEYS.has(p.split('.')[0])) {
warn(`companies/${cname}.yaml 与 roles/${rname}.yaml 同时定义了 "${p}"(合并时 roles 层覆盖 companies 层,请确认是否有意)`);
}
}
}
}
// 变体覆盖数量提醒(防 override 滥用导致"维度共享"失效)
for (const v of data.variants) {
const n = keyPaths(v.overrides || {}).size;
if (n > 12) warn(`[variants] ${v.company}-${v.role} 的 overrides 有 ${n} 个字段,数量偏多,请确认是否应下沉到维度层`);
}
// 3. dist 产物检查
const expected = [];
for (const v of views) {
expected.push(path.join(v.companyId, v.roleId, 'index.html'));
expected.push(path.join('txt', v.companyId, v.roleId, 'index.txt'));
}
expected.push('index.html', 'txt/index.html', 'css/base.css', 'css/theme-private.css', 'css/theme-soe.css', 'css/nav.css');
for (const rel of expected) {
if (!fs.existsSync(path.join(DIST, rel))) err(`缺少构建产物 dist/${rel.replace(/\\/g, '/')}`);
}
const LEAK = /\{\{|\}\}|undefined|\[object Object\]|(^|[^A-Za-z])NaN([^A-Za-z]|$)/;
const titles = new Set();
for (const rel of expected.filter((f) => /\.(html|txt)$/.test(f))) {
const file = path.join(DIST, rel);
if (!fs.existsSync(file)) continue;
const text = fs.readFileSync(file, 'utf8');
const m = text.match(LEAK);
if (m) err(`dist/${rel.replace(/\\/g, '/')} 疑似模板残留或非法值:"${m[0]}"`);
if (rel.endsWith('.html') && rel !== 'index.html' && rel !== 'txt/index.html') {
const t = text.match(/<title>([^<]+)<\/title>/);
if (t) titles.add(t[1]);
}
if (rel.endsWith('.txt') && !text.includes('【基本信息】')) err(`dist/${rel.replace(/\\/g, '/')} 缺少【基本信息】段`);
}
if (views.length && titles.size !== views.length) err(`简历页 <title> 不唯一(${titles.size} 个不同标题 / ${views.length} 份简历)`);
// ---------- 汇总 ----------
for (const w of warnings) console.log(`[警告] ${w}`);
for (const e of errors) console.log(`[错误] ${e}`);
console.log(`检查完成:${views.length} 份视图,${errors.length} 个错误,${warnings.length} 个警告`);
if (errors.length) process.exit(1);