소스 검색

feat: 配置 git pre-commit hook — 提交前自动运行 Biome 检查

使用 .githooks/ 目录 + core.hooksPath 方案,零依赖。
prepare script 确保 bun install 后自动激活。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
claude-code-best 3 주 전
부모
커밋
4319afc08f
3개의 변경된 파일25개의 추가작업 그리고 2개의 파일을 삭제
  1. 22 0
      .githooks/pre-commit
  2. 1 1
      TODO.md
  3. 2 1
      package.json

+ 22 - 0
.githooks/pre-commit

@@ -0,0 +1,22 @@
+#!/bin/sh
+# pre-commit hook: 对暂存的文件运行 Biome 检查
+# 仅检查 src/ 下的 .ts/.tsx/.js/.jsx 文件
+
+STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep -E '^src/.*\.(ts|tsx|js|jsx)$')
+
+if [ -z "$STAGED_FILES" ]; then
+  exit 0
+fi
+
+echo "Running Biome check on staged files..."
+
+# 使用 biome check 对暂存文件进行检查(仅 lint,不自动修复)
+echo "$STAGED_FILES" | xargs bunx biome check --no-errors-on-unmatched
+
+if [ $? -ne 0 ]; then
+  echo ""
+  echo "Biome check failed. Fix errors or use --no-verify to bypass."
+  exit 1
+fi
+
+exit 0

+ 1 - 1
TODO.md

@@ -19,4 +19,4 @@
 
 - [x] 代码格式化与校验
 - [ ] 冗余代码检查
-- [ ] git hook 的配置
+- [x] git hook 的配置

+ 2 - 1
package.json

@@ -15,7 +15,8 @@
         "prepublishOnly": "bun run build",
         "lint": "biome check src/",
         "lint:fix": "biome check --fix src/",
-        "format": "biome format --write src/"
+        "format": "biome format --write src/",
+        "prepare": "git config core.hooksPath .githooks"
     },
     "dependencies": {
         "@alcalzone/ansi-tokenize": "^0.3.0",