Files
ai-proj-helper/skills-req/req-test-gate-plugin/skills/templates/pre-commit-config.md
John Qiu b9c808cce0 feat(req-test-gate): 集成 Harness Engineering 工程约束方法论
将项目级的 Ratchet/约定检测方法论融入 req-test-gate 技能,
通过 /req 流程三个节点自动触发(dev 环境检测、cr 约定建议、test Gate 0B),
无需手动记忆执行。

新增文档:harness-engineering.md、ratchet-pattern.md、convention-flow.md、
project-bootstrap.md 及 4 个模板(ratchet/convention 脚本、GATES.md、pre-commit)。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-26 11:34:42 +10:30

136 lines
2.7 KiB
Markdown
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.
# Pre-commit Configuration Template
husky + lint-staged 配置模板三种变体Node.js 项目、Go 项目、Monorepo。
---
## 变体 1: Node.js 项目React / Vue / 纯 TS
### 安装
```bash
npm install -D husky lint-staged prettier eslint
npx husky init
echo "npx lint-staged" > .husky/pre-commit
```
### package.json
```json
{
"lint-staged": {
"*.{ts,tsx}": [
"eslint --fix --max-warnings 0",
"prettier --write"
],
"*.{css,less,scss}": [
"prettier --write"
],
"*.{json,md}": [
"prettier --write"
]
}
}
```
---
## 变体 2: Go 项目
### 安装
```bash
# Go 项目需要 package.json 来驱动 husky
npm init -y
npm install -D husky lint-staged
npx husky init
echo "npx lint-staged" > .husky/pre-commit
```
### package.json
```json
{
"lint-staged": {
"*.go": [
"gofmt -s -w",
"bash -c 'go vet ./...'"
]
}
}
```
### 可选golangci-lint更严格
```json
{
"lint-staged": {
"*.go": [
"gofmt -s -w",
"bash -c 'go vet ./...'",
"bash -c 'golangci-lint run --fix'"
]
}
}
```
---
## 变体 3: MonorepoGo + Node.js
### 安装(根目录)
```bash
npm install -D husky lint-staged
npx husky init
echo "npx lint-staged" > .husky/pre-commit
```
### package.json根目录
```json
{
"lint-staged": {
"frontend/**/*.{ts,tsx}": [
"bash -c 'cd frontend && npx eslint --fix --max-warnings 0'",
"bash -c 'cd frontend && npx prettier --write'"
],
"frontend/**/*.{css,less,json}": [
"bash -c 'cd frontend && npx prettier --write'"
],
"backend/**/*.go": [
"gofmt -s -w",
"bash -c 'cd backend && go vet ./...'"
]
}
}
```
### 注意事项
- `bash -c 'cd xxx && ...'` 确保命令在正确的子目录中执行
- lint-staged 传递的文件路径是相对于根目录的,部分工具(如 `go vet`)需要在子目录执行
- 如果前端和后端各自有 `package.json`lint-staged 仍然在根目录配置
---
## 验证配置
```bash
# 修改一个文件后测试 pre-commit hook
echo "// test" >> some-file.ts
git add some-file.ts
git commit -m "test: verify pre-commit hook"
# 应该看到 lint-staged 执行 ESLint + Prettier
# 如果有 lint 错误commit 会被阻止
```
## 常见问题
| 问题 | 原因 | 解决 |
|------|------|------|
| lint-staged 不执行 | `.husky/pre-commit` 内容不对 | 确认内容为 `npx lint-staged` |
| ESLint 报错太多 | 旧项目首次启用 | 先 `npx eslint --fix` 全量修复,再启用 hook |
| gofmt 修改后 commit 内容不一致 | lint-staged 自动 format 后需要 re-stage | lint-staged 自动处理,无需手动 |
| Monorepo 路径错误 | lint-staged 传递根目录相对路径 | 使用 `bash -c 'cd sub && ...'` 包装 |