Claude Code Hooks 自動化建構器:每次編輯後自動跑格式化/lint/測試
用 .claude/settings.json 的 hooks,在 Claude Code 的特定生命週期事件(PostToolUse 編輯後、PreToolUse 跑指令前、Stop 收尾時)自動執行格式化、lint、typecheck 或測試——產出正確的 JSON 設定 + 可執行的 hook 腳本,並用 exit 2 在危險操作前硬擋。
CLAUDE.md 只是『建議』,模型不一定每次照做;hooks 是『強制』,由 harness 在固定事件確實執行。這支 meta-prompt 幫你產出正確的 .claude/settings.json hook 設定+腳本:Claude 每改完檔自動 prettier+lint,跑 rm -rf 前直接 exit 2 擋下。
[ Log in to see the full prompt ]Sign up free to see the full prompt, copy it, save it, and join the discussion. Free content unlocks on login; Pro content is a separate subscription.
【何時用】當某件事『非做不可、不能靠模型自律』時——例如每次編輯後必須格式化、收尾前必須測試通過、或某些危險指令必須被擋。官方明確說:CLAUDE.md 是模型嘗試遵守的『建議』、不保證執行;要在固定時點強制執行就用 hooks。【為何有效】(1) 它把每個目標對應到正確事件:PostToolUse(matcher Edit|Write) 在改檔後跑格式化/lint、PreToolUse(matcher Bash) 是唯一能用 exit 2『擋下』工具呼叫的地方、Stop 當收尾測試閘門;(2) 給出真實 settings.json 結構(hooks→事件→matcher→command)與真實 stdin 欄位(tool_name / tool_input / cwd),command hook 從 stdin 收 JSON、靠 exit code 控流(0 過、2 阻擋並把 stderr 當理由回給 Claude);(3) PostToolUse 只處理 tool_input 裡那個被改的檔、不亂格式化整個 repo。【技巧】hooks 用你的完整權限自動執行、專案層要先接受 workspace-trust 才生效,所以腳本要最小化、變數加引號、別把 tool_input 直接 eval。驗證方式:隨手改一個檔看格式化有沒有跑、故意打一個被禁指令看是否被 exit 2 擋下。
// .claude/settings.json (merged) { "hooks": { "PostToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/format.sh", "timeout": 30 } ] } ], "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/guard.sh", "timeout": 5 } ] } ] } } # .claude/hooks/format.sh #!/usr/bin/env bash set -euo pipefail file=$(jq -r '.tool_input.file_path // empty') [[ "$file" == *.ts || "$file" == *.tsx ]] || exit 0 prettier --write "$file" >/dev/null if ! eslint "$file"; then echo "eslint failed on $file — fix before continuing" >&2; exit 2; fi # .claude/hooks/guard.sh #!/usr/bin/env bash cmd=$(jq -r '.tool_input.command // empty') if echo "$cmd" | grep -Eq 'rm -rf /|git push .*--force'; then echo "Blocked dangerous command: $cmd" >&2; exit 2 fi exit 0
Suno Engineer's Mindset: 4 Steps to a Song That Doesn't Sound Like AI
A studio engineer's breakdown of Suno's fatal weaknesses (fried vocals, high-frequency artifacts), plus a 4-step DAW workflow and a Suno Studio cleanup prompt.
5 Claude Weekly Workflows That Stuck After 6 Months
Proposal generator / meeting processor / content repurposer / Friday review / shutdown reset — out of 40 I tried, only these 5 survived, each saving 30+ minutes per run.