把 runner/ 打包成可直接部署的容器镜像,Linux 服务器 / 群晖 / 云主机上 不用再手动装 Node 与浏览器。 - Dockerfile:node:22-bookworm-slim + 官方 google-chrome-stable。 刻意不用发行版 chromium —— 它不带 H.264/AAC 专有编解码器,课程视频会 一直停在 0 秒;同时满足 preflight.mjs 对 Node 22+ 的硬性要求。 构建期完成 npm ci + npm run build,并实跑一次 headless 验证浏览器可用; CHROME_DEB_URL 构建参数供国内换镜像源。 - docker/entrypoint.sh:建 profile / 日志目录;首参为 sh/node 等时直接 exec 便于排查;无参数且无凭据时降级成 --check,避免容器无意义重启空转。 - docker-compose.yml:挂载 ./.run-profile 与 ./run-logs,shm_size 512m, init: true,日志轮转。restart 用 on-failure:3 而非 always —— 运行器刷完课 会以 0 正常退出,always 会造成「刷完 → 重启 → 再刷完」的热循环。 - .dockerignore:排除 node_modules / .run-profile / run-logs / .env, 凭据不进镜像层。 - .env.example:账号模板;.gitignore 加 !.env.example 白名单。 - .gitattributes:*.sh / Dockerfile / docker-compose.yml 强制 LF —— 带 CRLF 时 shebang 会变成 "/bin/sh\r",容器启动直接 exec format error。 - scripts/check.mjs:静态检查扩展到容器入口,跑 sh -n 并对 CRLF 报错 (Windows 无 sh 时只校验行尾)。29 个文件全绿。 - README:新增「容器(Docker)」小节,含 build / 自检 / run / compose 流程 与 8 条注意事项(卷权限、/dev/shm、沙箱、验证码、时区、退出码与重启策略)。
144 lines
5.4 KiB
JavaScript
144 lines
5.4 KiB
JavaScript
#!/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', 'docker'];
|
||
/** 顶层要单独检查的文件(构建产物不入源码校验,它在 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$|\.sh$/i.test(ent.name)) {
|
||
out.push(p);
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
const jsTargets = [];
|
||
const cmdTargets = [];
|
||
const shTargets = [];
|
||
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 if (/\.sh$/i.test(f)) shTargets.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);
|
||
}
|
||
}
|
||
|
||
// ---- 3. 容器入口脚本:sh -n 语法 + 必须是 LF 行尾 ----
|
||
// 容器的 entrypoint 若带 CRLF,shebang 会变成 "/bin/sh\r",直接报 exec format error。
|
||
// Windows 上不一定有 sh(纯 CMD / PowerShell 环境),找不到就跳过语法检查,但行尾照样校验。
|
||
const hasSh = spawnSync('sh', ['-c', 'exit 0'], { stdio: 'ignore' }).status === 0;
|
||
for (const file of shTargets.sort()) {
|
||
const rel = path.relative(ROOT, file);
|
||
const buf = fs.readFileSync(file);
|
||
const bad = [];
|
||
const crlf = buf.indexOf('\r\n') >= 0;
|
||
if (crlf) bad.push('含 CRLF 行尾;容器里的 sh 会把 shebang 读成 "/bin/sh\\r" 而报 exec format error');
|
||
if (hasSh) {
|
||
const r = spawnSync('sh', ['-n', file], { encoding: 'utf8' });
|
||
if (r.status !== 0) bad.push((r.stderr || '').trim());
|
||
}
|
||
if (bad.length) {
|
||
failed++;
|
||
console.error(' FAIL ' + rel);
|
||
for (const b of bad) console.error(' - ' + b);
|
||
}
|
||
}
|
||
|
||
const total = jsTargets.length + cmdTargets.length + shTargets.length;
|
||
if (failed) {
|
||
console.error('\n静态检查:' + failed + '/' + total + ' 个文件失败');
|
||
process.exit(1);
|
||
}
|
||
console.log('静态检查:' + total + '/' + total + ' 个文件通过'
|
||
+ '(JS ' + jsTargets.length + ' · 批处理 ' + cmdTargets.length
|
||
+ ' · 容器入口 ' + shTargets.length + (hasSh ? '' : '(当前环境无 sh,仅校验行尾)') + ')');
|