Files
materials/tools/check-portal.js
huanghaosheng 84aa0143a8 refactor: 板块 03 改为「SRE 工程师」专属板块,站点迁至 SRE工程师/sre-learning-path/
- 取消「学习路径 / learning-path」模块,新增 id=sre、目录=SRE工程师 的「SRE 工程师」板块:
  统一归档 SRE 相关资料(学习路径 + 排障速查 / SLO 模板 / 值班与复盘模板 / 面试题 / 书单等)
- 学习路线/sre-learning-path/ -> SRE工程师/sre-learning-path/(git mv,404 个文件保留改名历史),
  搬空的 学习路线/ 目录删除
- 路由改为 #/sre/sre-learning-path,返回入口标签改为「SRE 工程师 › SRE 从零到成熟学习路径」
  (399 个页面由 tools/add-back-links.js 重算)
- docker / linux / nginx 的外链、build-nav.js 生成器与自检白名单,.sre-s23 脚手架,
  以及 README / DEPLOY.md(含 nginx 中文目录的百分号编码示例)全部同步到新路径

自检:check-portal(4 个板块 / 6 份资料)、add-back-links --check(454 页)全绿;
SRE 站点 399 页与 docker、linux、nginx 的 check-pages、.sre-s23 四项体检均通过。
2026-10-15 11:55:58 +08:00

159 lines
6.0 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.
#!/usr/bin/env node
/* ============================================================================
* 资料库导航 · 注册表自检
* ----------------------------------------------------------------------------
* 校验 assets/portal.data.js 里登记的每一条资料在磁盘上真实存在,
* 避免导航页出现死链(这也是纯静态 + 手写注册表的唯一风险点)。
*
* node tools/check-portal.js
*
* 检查项:
* 1. 模块 / 资料 id 唯一,path、dir 在仓库内(不允许绝对路径或 .. 逃逸)
* 2. 每个 path 指向的文件存在,且是 .html;每个 dir 指向的目录存在
* 3. path / dir 位于该模块自己的顶层目录下
* (模块目录默认等于模块 id;id 需要英文 URL 时可以另写 `dir: 'SRE工程师'` 指定)
* 4. 统计条目数与未登记资料(interview/ 这类空目录允许为空)
* 退出码:0 全部通过,1 有问题。
* ==========================================================================*/
'use strict';
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const ROOT = path.resolve(__dirname, '..');
const DATA_FILE = path.join(ROOT, 'assets', 'portal.data.js');
const problems = [];
const warnings = [];
const fail = (m) => problems.push(m);
const warn = (m) => warnings.push(m);
/* ------------------------------------------------------------ 读取注册表 */
if (!fs.existsSync(DATA_FILE)) {
console.error('✗ 找不到 ' + path.relative(ROOT, DATA_FILE));
process.exit(1);
}
const sandbox = { window: {} };
try {
vm.createContext(sandbox);
vm.runInContext(fs.readFileSync(DATA_FILE, 'utf8'), sandbox, { filename: 'portal.data.js' });
} catch (e) {
console.error('✗ portal.data.js 解析失败:' + e.message);
process.exit(1);
}
const DATA = sandbox.window.PORTAL_DATA;
if (!DATA || !Array.isArray(DATA.modules)) {
console.error('✗ portal.data.js 里没有 window.PORTAL_DATA.modules');
process.exit(1);
}
/* ------------------------------------------------------------ 工具 */
const isPlain = (p) => !path.isAbsolute(p) && !/^[a-zA-Z]:/.test(p);
function inside(p, label, where) {
if (typeof p !== 'string' || !p.trim()) { fail(`${where}:${label} 为空`); return false; }
if (!isPlain(p)) { fail(`${where}:${label} 必须是相对路径,实际为 "${p}"`); return false; }
if (p.split(/[\\/]/).includes('..')) { fail(`${where}:${label} 不允许出现 ..("${p}")`); return false; }
return true;
}
function exists(rel, kind) {
const abs = path.join(ROOT, rel);
let st;
try { st = fs.statSync(abs); } catch (e) { return null; }
if (kind === 'dir' && !st.isDirectory()) return null;
if (kind === 'file' && !st.isFile()) return null;
return st;
}
/* ------------------------------------------------------------ 主检查 */
const modIds = new Set();
let total = 0;
const rows = [];
for (const m of DATA.modules) {
const where = `模块 ${m && m.id ? m.id : '(缺少 id)'}`;
if (!m || typeof m !== 'object') { fail(`${where}:不是对象`); continue; }
for (const f of ['id', 'name', 'icon', 'num', 'accent', 'accentSoft']) {
if (!m[f]) fail(`${where}:缺少字段 ${f}`);
}
if (m.id) {
if (modIds.has(m.id)) fail(`${where}:模块 id 重复`);
modIds.add(m.id);
}
if (!Array.isArray(m.entries)) { fail(`${where}:entries 必须是数组`); continue; }
// 模块的磁盘目录:默认与 id 同名,id 是英文时可以用 dir 指向中文目录
const modDir = (m.dir || m.id || '').replace(/\/+$/, '');
if (m.dir && !inside(m.dir, 'dir', where)) continue;
if (m.temp) {
if (inside(m.temp.path, 'temp.path', where) && !exists(m.temp.path, 'file')) {
fail(`${where}:临时页 ${m.temp.path} 不存在`);
}
}
const entryIds = new Set();
let n = 0;
for (const e of m.entries) {
n++;
const w = `${where} › 资料 ${e && e.id ? e.id : '(缺少 id)'}`;
if (!e || typeof e !== 'object') { fail(`${w}:不是对象`); continue; }
for (const f of ['id', 'name', 'desc']) {
if (!e[f]) fail(`${w}:缺少字段 ${f}`);
}
if (e.id) {
if (entryIds.has(e.id)) fail(`${w}:同一模块内 id 重复`);
entryIds.add(e.id);
}
if (e.path) {
if (inside(e.path, 'path', w)) {
if (!exists(e.path, 'file')) fail(`${w}:path ${e.path} 不存在`);
else if (!/\.html?$/i.test(e.path)) warn(`${w}:path ${e.path} 不是 .html,点开会是下载或源码`);
if (modDir && !e.path.startsWith(modDir + '/')) {
fail(`${w}:path ${e.path} 不在 ${modDir}/ 目录下`);
}
}
} else {
fail(`${w}:缺少 path`);
}
if (e.dir && inside(e.dir, 'dir', w) && !exists(e.dir, 'dir')) {
fail(`${w}:dir ${e.dir} 不是已存在的目录`);
}
for (const s of e.stats || []) {
if (!Array.isArray(s) || s.length !== 2) fail(`${w}:stats 每一项必须是 [值, 说明]`);
}
}
total += n;
rows.push([m.id, m.name, n, m.statusText || m.status || '']);
// 模块自己的目录应当存在(interview / knowledge / SRE工程师 / project)
if (modDir && !exists(modDir, 'dir')) fail(`${where}:目录 ${modDir}/ 不存在`);
}
/* ------------------------------------------------------------ 输出 */
const line = '─'.repeat(64);
console.log(line);
console.log('资料库导航 · 注册表自检');
console.log(line);
for (const [id, name, n, st] of rows) {
console.log(` ${id.padEnd(12)} ${String(n).padStart(2)} 份 ${st.padEnd(10)} ${name}`);
}
console.log(line);
console.log(` 模块 ${rows.length} 个 · 资料 ${total} 份`);
if (warnings.length) {
console.log('\n⚠ 提示:');
warnings.forEach((w) => console.log(' - ' + w));
}
if (problems.length) {
console.log('\n✗ 问题:');
problems.forEach((p) => console.log(' - ' + p));
console.log(`\n共 ${problems.length} 个问题,请修正 assets/portal.data.js 或补齐文件。`);
process.exit(1);
}
console.log('\n✓ 全部通过:注册表里的每个入口都能在磁盘上找到。');