Claude Code Hooks में Mastery: Auto-Format, Auto-Test, और बहुत कुछ
Claude Code hooks से auto-formatting और auto-testing setup करना सीखें। Practical configuration examples और real-world use cases शामिल हैं।
Hooks क्या हैं?
Claude Code hooks आपको specific actions से पहले या बाद में automatically custom commands run करने देते हैं। आप file save के बाद auto-formatting, code changes के बाद auto-testing, और बहुत कुछ setup कर सकते हैं।
Hooks .claude/settings.json में define होते हैं और Claude Code कुछ specific operations perform करने पर shell commands execute करते हैं।
Hook Types
Claude Code इन event points पर hooks support करता है:
| Hook Event | कब Fire होता है |
|---|---|
PreToolUse | किसी tool के execute होने से पहले |
PostToolUse | किसी tool के execute होने के बाद |
Notification | जब notification send होता है |
Stop | जब Claude Code respond करना finish करता है |
Basic Configuration
Hooks .claude/settings.json के hooks field में define होते हैं:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx prettier --write \"$CLAUDE_FILE_PATH\""
}
]
}
}
Configuration Structure
- matcher: Tool name match करने वाला regex pattern जो hook trigger करता है
- command: Execute होने वाला shell command
Use Case 1: Auto-Formatting
किसी भी file edit के बाद automatically Prettier run करें:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx prettier --write \"$CLAUDE_FILE_PATH\""
}
]
}
}
इस setup से, हर बार जब Claude Code कोई file create या edit करता है, Prettier automatically उसे format कर देता है। आपकी team की coding style बिना किसी manual effort के consistent रहती है।
Use Case 2: Auto-Linting
ESLint auto-fix integrate करने का तरीका:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx eslint --fix \"$CLAUDE_FILE_PATH\" 2>/dev/null || true"
}
]
}
}
|| true suffix lint errors को hook fail होने से रोकता है, जिससे Claude Code processing continue कर सकता है।
Use Case 3: Change पर Auto-Testing
File edits के बाद automatically related tests run करें और results feed back करें:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx vitest related \"$CLAUDE_FILE_PATH\" --run 2>&1 | tail -20"
}
]
}
}
Vitest का related option सिर्फ changed file से related tests run करता है, जो full test suite से बहुत faster feedback देता है।
Use Case 4: Auto Type-Checking
File changes के बाद TypeScript type checking run करें:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx tsc --noEmit 2>&1 | head -30"
}
]
}
}
Use Case 5: Dangerous Commands Block करना
एक PreToolUse hook जो risky commands का execution रोकता है:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "if echo \"$CLAUDE_TOOL_INPUT\" | grep -qE 'rm -rf|drop table|git push.*force'; then echo 'BLOCKED: Dangerous command detected' >&2; exit 1; fi"
}
]
}
}
जब hook exit code 1 के साथ exit करता है, Claude Code tool execution पूरी तरह skip कर देता है।
Use Case 6: Completion Notifications
Claude Code task finish करने पर notification भेजें:
{
"hooks": {
"Stop": [
{
"matcher": "",
"command": "notify-send 'Claude Code' 'Task completed' 2>/dev/null; echo 'Done'"
}
]
}
}
macOS पर, आप यह use कर सकते हैं:
{
"hooks": {
"Stop": [
{
"matcher": "",
"command": "osascript -e 'display notification \"Task completed\" with title \"Claude Code\"'"
}
]
}
}
Multiple Hooks Combine करना
Real projects में, कई hooks combine करना सबसे effective approach है:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "if echo \"$CLAUDE_TOOL_INPUT\" | grep -qE 'rm -rf /'; then exit 1; fi"
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx prettier --write \"$CLAUDE_FILE_PATH\" 2>/dev/null || true"
},
{
"matcher": "Write|Edit",
"command": "npx eslint --fix \"$CLAUDE_FILE_PATH\" 2>/dev/null || true"
}
],
"Stop": [
{
"matcher": "",
"command": "echo '✓ Task completed'"
}
]
}
}
Hooks लिखने की Tips
1. Execution Time Short रखें
Hooks synchronously run होते हैं, इसलिए heavy operations Claude Code के responses slow कर देंगे। Test scope narrow करने के लिए related जैसे options use करें, और output trim करने के लिए head या tail।
2. Errors को Gracefully Handle करें
Error से exit होने वाला hook Claude Code के workflow को disrupt कर सकता है। चीज़ों को safe रखने के लिए || true और 2>/dev/null use करें।
3. Environment Variables Use करें
Hooks में available environment variables का फायदा उठाएं। $CLAUDE_FILE_PATH आपको operate हो रही file का path देता है।
Conclusion
Hooks आपको Claude Code के workflow को heavily customize करने देते हैं। Auto-formatting और auto-testing का combination code quality maintain करने के लिए especially powerful है। Prettier auto-formatting से शुरू करें और वहाँ से build करें।