Files
Genarrative/scripts/lint-staged-rustfmt.mjs
T
suzmii dd7cf401a9 补 lint-staged 的 Rust 守卫:pre-commit 也跑 check:rustfmt
- 新增 scripts/lint-staged-rustfmt.mjs:对 server-rs 与 apps/ai-game-creator-shell/src-tauri 两个 workspace 跑 cargo fmt --all --manifest-path <m> -- --check,只查不改
- 为什么需要包装脚本:lint-staged 会把命中的暂存文件路径追加到命令末尾,而 cargo fmt 只按 workspace 粒度格式化、不接受文件参数,直接写成 npm run check:rustfmt 会被多余参数打断;脚本因此忽略 argv,并按 workspace 逐个检查
- package.json 的 lint-staged 配置新增 "*.rs": ["node scripts/lint-staged-rustfmt.mjs"]
- 成因:原配置只覆盖 *.{js,mjs,cjs,ts,tsx},Rust 格式在本地完全没有守卫,唯一防线是 CI 的 check:rustfmt,本 PR 已因此红过一次(见 15660a98b)
- 已实测:把包装脚本直接跑一遍 exit 0;lint-staged 分派层面确认 "*.rs" 任务会被真的触发(对 6 个 .rs 文件跑通);脚本本身过 eslint 与 prettier --check
2026-09-11 21:24:43 +08:00

33 lines
1.2 KiB
JavaScript

import { spawnSync } from 'node:child_process';
import process from 'node:process';
// lint-staged 会把命中的暂存文件路径追加到命令末尾,而 `cargo fmt` 只按 workspace 粒度格式化、
// 不接受文件参数(也做不到「只格式化某个文件」),所以这里忽略 argv,直接对两个 workspace 跑
// `--check`。只查不改:pre-commit 不应该自动改写别人正在改的 Rust 文件。
const workspaces = [
'server-rs/Cargo.toml',
'apps/ai-game-creator-shell/src-tauri/Cargo.toml',
];
for (const manifestPath of workspaces) {
const result = spawnSync(
'cargo',
['fmt', '--all', '--manifest-path', manifestPath, '--', '--check'],
{ stdio: 'inherit', shell: process.platform === 'win32' },
);
if (result.error) {
process.stderr.write(
`Rust 格式检查无法执行:${manifestPath}: ${result.error.message}\n`,
);
process.exit(1);
}
if (result.status !== 0) {
process.stderr.write(
`Rust 格式检查未通过:${manifestPath}\n` +
`本地复现:npm run check:rustfmt\n` +
`自动修复:cargo fmt --all --manifest-path ${manifestPath}\n`,
);
process.exit(result.status ?? 1);
}
}