Files
yun-xue-tang-assistant/scripts/check.mjs
T
void 65bf63ea6a feat: v2.9.0 终端实时面板,学习时长与学分统计
新增 runner/tui.mjs(~470 行):前台运行时自动开一块实时面板,
框线布局显示状态 / 课程 / 学时 / 课时 / 视频进度条 / 进度 / 最近日志。

- 学习统计 runner/study.mjs:只累计视频真在播的时长(暂停、重试退避、
  页面加载都不计),学分按课程去重(重试 / 刷新不重复计)
- 页面侧新增 credit 快照:平台只把本门学分写在倒计时文案里,
  学完文案就消失,所以边播边记
- log.mjs 增加 setConsoleSink:面板接管终端那一路,日志文件仍是纯文本
- 面板里按 q 等价 Ctrl+C,走 stop.request 同一条优雅收尾路径
- 尺寸自适应:宽度跟随终端(不封顶 120 列),无 resize 事件的平台
  靠定时刷新发现尺寸变化;变窄时按旧帧在新宽度下的折行数回退光标,
  折完超过窗口高就整屏重置;宽度 / 高度不足按重要性丢字段
- 非 TTY(重定向 / 管道 / 计划任务 / --check)自动降级为逐行日志,
  不写 ANSI 控制字符;--no-tui / YUN_XUE_TANG_TUI=0 可手动关闭,
  --no-color / NO_COLOR 关颜色
- 监控轮询从 2000ms 调到 1000ms,与面板刷新对齐
- supervisor 收尾时先收面板再逐行打退出日志,并汇总本次学习时长 / 学分
- check.mjs 增加裸 CR 检查:脚本里的 config\runner 曾被写成裸 CR,
  变成 config<CR>unner 导致找不到配置文件

自测:npm run test:tui(渲染 / 列宽 / 极端尺寸)、npm run test:study(假时钟口径)
2026-10-26 21:49:13 +08:00

120 lines
4.3 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 一起收;scripts/ 也在里面,含本文件自己) */
const DIRS = ['runner', 'dev', 'scripts', 'config'];
/** 顶层要单独检查的文件(构建产物不入源码校验,它在 scripts/ 下重新生成后自然被覆盖) */
const FILES = ['yun-xue-tang-assistant.user.js'];
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);
}
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;
let loneCr = 0;
for (let i = 0; i < buf.length; i++) {
if (buf[i] === 0x0a) {
lf++;
if (i > 0 && buf[i - 1] === 0x0d) crlf++;
} else if (buf[i] === 0x0d && buf[i + 1] !== 0x0a) {
loneCr++;
}
}
// 裸 CR(不是 CRLF 的一部分)会把下一个字符吃掉:'config\runner' 里的反斜杠
// 曾经变成 CR,报“configunner.config.mjs not found”
if (loneCr) {
bad.push('有 ' + loneCr + ' 处裸 CR(不是 CRLF 的 CR);'
+ '它会吞掉后面的字符,例如 config\\runner 变成 config<CR>unner');
}
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 + ')');