2026-10-14 22:42:38 +08:00
|
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
/* ============================================================================
|
|
|
|
|
|
* 资料库导航 · 注册表自检
|
|
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
|
|
* 校验 assets/portal.data.js 里登记的每一条资料在磁盘上真实存在,
|
|
|
|
|
|
* 避免导航页出现死链(这也是纯静态 + 手写注册表的唯一风险点)。
|
|
|
|
|
|
*
|
|
|
|
|
|
* node tools/check-portal.js
|
|
|
|
|
|
*
|
|
|
|
|
|
* 检查项:
|
|
|
|
|
|
* 1. 模块 / 资料 id 唯一,path、dir 在仓库内(不允许绝对路径或 .. 逃逸)
|
|
|
|
|
|
* 2. 每个 path 指向的文件存在,且是 .html;每个 dir 指向的目录存在
|
2026-10-14 23:33:29 +08:00
|
|
|
|
* 3. path / dir 位于该模块自己的顶层目录下
|
|
|
|
|
|
* (模块目录默认等于模块 id;id 需要英文 URL 时可以另写 `dir: '学习路线'` 指定)
|
2026-10-14 22:42:38 +08:00
|
|
|
|
* 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; }
|
|
|
|
|
|
|
2026-10-14 23:33:29 +08:00
|
|
|
|
// 模块的磁盘目录:默认与 id 同名,id 是英文时可以用 dir 指向中文目录
|
|
|
|
|
|
const modDir = (m.dir || m.id || '').replace(/\/+$/, '');
|
|
|
|
|
|
if (m.dir && !inside(m.dir, 'dir', where)) continue;
|
|
|
|
|
|
|
2026-10-14 22:42:38 +08:00
|
|
|
|
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,点开会是下载或源码`);
|
2026-10-14 23:33:29 +08:00
|
|
|
|
if (modDir && !e.path.startsWith(modDir + '/')) {
|
|
|
|
|
|
fail(`${w}:path ${e.path} 不在 ${modDir}/ 目录下`);
|
2026-10-14 22:42:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} 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 || '']);
|
|
|
|
|
|
|
2026-10-14 23:33:29 +08:00
|
|
|
|
// 模块自己的目录应当存在(interview / knowledge / 学习路线 / project)
|
|
|
|
|
|
if (modDir && !exists(modDir, 'dir')) fail(`${where}:目录 ${modDir}/ 不存在`);
|
2026-10-14 22:42:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------ 输出 */
|
|
|
|
|
|
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✓ 全部通过:注册表里的每个入口都能在磁盘上找到。');
|