Files
yun-xue-tang-assistant/dev/check.mjs
T
void ae117222df feat: v2.8.0 无头运行器、考试自动交卷、改为退避重试不跳课
新增无头运行能力,解决 Linux 无图形化服务器上无法常驻的问题;同时改掉
"遇到考试课/无进展课就跳过"的策略,改为在同门课上退避重试。

- runner/:自己起无头 Chromium,用 CDP 把构建产物注入页面主世界(等价
  @grant none),不装插件、不需要桌面环境或 Xvfb;含浏览器探测与启动参数、
  持久化 profile、陈旧 SingletonLock 清理、优雅退出、日志轮转、启动预检
- runner.config.mjs / start-headless.cmd:通用配置 + Windows 一键入口;
  凭据只走 YUN_XUE_TANG_USR / YUN_XUE_TANG_PWD 环境变量
- src/exam.js:含考试章节的课程自动进考试 → 开始/继续 → 空卷提交 → 确定,
  交卷后靠平台记账推进课程;maxExamTries 限制同门课交卷次数,防不记账时死循环
- 去掉跳过与黑名单:没学完就退避重试(30s 起翻倍,最长 5 分钟),
  同门课超过 maxCourseRetries 次才停整轮并提示,替代原 maxConsecutiveFailures
- 每完成一门都重新拉列表接口取平台实时状态再选下一门,避免用陈旧队列
- 学习进度改存 sessionStorage(yun-xue-tang-run-state-v3),整页跳转/刷新可续;
  运行开关不跨会话,浏览器重启后必须显式开跑
- dev/:新增 real-cdp / real-run / real-test / real-e2e 实跑脚本与 check.mjs
  统一静态检查(JS 22 + 批处理 2),替代逐文件 node --check
- docs/evidence/headless-linux-evidence.txt + 实跑截图,README 补无头部署章节
2026-10-25 23:04:40 +08:00

116 lines
3.9 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
/**
* 静态检查:对仓库里会直接 `node` 执行的脚本跑 `node --check`,
* 并对批处理文件做两项硬性检查(这两项一旦坏掉就极难排查):
* 1. 必须是纯 ASCII —— cmd.exe 按当前代码页逐字节读批处理,
* 多字节字符(中文)会让行偏移错位,报出 "'鍏?npm' is not recognized" 这种怪错;
* 2. 必须用 CRLF 行尾 —— 只有 LF 时 cmd.exe 会把上一行的尾部当成命令的一部分,
* 同样报出莫名其妙的 "'01' is not recognized"。
*
* 以前 package.json 的 check 脚本是一长串 `node --check a && node --check b && …`,
* 每次新增模块都要手改那行命令(漏了就默默不检查)。现在改为自动遍历目录。
*
* 用法:npm run check
*/
import fs from 'node:fs';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
/** 要遍历的目录(JS 与 bat 一起收) */
const DIRS = ['runner', 'dev'];
/** 顶层要单独检查的文件(runner.config.mjs 是配置文件,也要求语法正确) */
const FILES = ['build.mjs', 'runner.config.mjs', 'yun-xue-tang-assistant.user.js'];
const TOP_CMD = ['start-headless.cmd'];
const SKIP_DIRS = new Set(['node_modules', '.git', '.run-profile', 'run-logs', '.run-tmp']);
function walk(dir, out = []) {
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
const p = path.join(dir, ent.name);
if (ent.isDirectory()) {
if (!SKIP_DIRS.has(ent.name)) walk(p, out);
} else if (/\.m?js$|\.cmd$|\.bat$/i.test(ent.name)) {
out.push(p);
}
}
return out;
}
const jsTargets = [];
const cmdTargets = [];
for (const d of DIRS) {
const dir = path.join(ROOT, d);
if (fs.existsSync(dir)) {
for (const f of walk(dir)) {
if (/\.m?js$/.test(f)) jsTargets.push(f);
else cmdTargets.push(f);
}
}
}
for (const f of FILES) {
const p = path.join(ROOT, f);
if (fs.existsSync(p)) jsTargets.push(p);
}
for (const f of TOP_CMD) {
const p = path.join(ROOT, f);
if (fs.existsSync(p)) cmdTargets.push(p);
}
let failed = 0;
// ---- 1. JS 语法 ----
for (const file of jsTargets.sort()) {
const r = spawnSync(process.execPath, ['--check', file], { encoding: 'utf8' });
if (r.status !== 0) {
failed++;
console.error(' FAIL ' + path.relative(ROOT, file));
console.error((r.stderr || '').trim());
}
}
// ---- 2. 批处理文件:纯 ASCII + CRLF ----
for (const file of cmdTargets.sort()) {
const rel = path.relative(ROOT, file);
const buf = fs.readFileSync(file);
const bad = [];
const nonAscii = [];
for (let i = 0; i < buf.length; i++) {
if (buf[i] > 0x7f) nonAscii.push(i);
}
if (nonAscii.length) {
const first = nonAscii[0];
const ctx = buf.slice(Math.max(0, first - 20), first + 20).toString('utf8');
bad.push('含 ' + nonAscii.length + ' 个非 ASCII 字节(首次出现在偏移 ' + first + ',上下文 “' + ctx + '”);'
+ 'cmd.exe 按代码页逐字节读取,中文会让行偏移错位');
}
let lf = 0;
let crlf = 0;
for (let i = 0; i < buf.length; i++) {
if (buf[i] === 0x0a) {
lf++;
if (i > 0 && buf[i - 1] === 0x0d) crlf++;
}
}
if (lf !== crlf) {
bad.push('有 ' + (lf - crlf) + ' 处裸 LF 行尾(共 ' + lf + ' 个 LF / ' + crlf + ' 个 CRLF);'
+ 'cmd.exe 会把上一行尾部当成命令的一部分');
}
if (bad.length) {
failed++;
console.error(' FAIL ' + rel);
for (const b of bad) console.error(' - ' + b);
}
}
const total = jsTargets.length + cmdTargets.length;
if (failed) {
console.error('\n静态检查:' + failed + '/' + total + ' 个文件失败');
process.exit(1);
}
console.log('静态检查:' + total + '/' + total + ' 个文件通过'
+ '(JS ' + jsTargets.length + ' · 批处理 ' + cmdTargets.length + ')');