🎓
บทที่ 2 · ⏱ 10 นาที
Hooks — ทำงานอัตโนมัติก่อนหรือหลัง action
ผมตั้ง hook ป้องกันไม่ให้ Claude แก้ .env หรือ run rm — ทำงานได้เร็วโดยไม่ห่วงเรื่องพังของ
Hooks คือคำสั่งที่รันอัตโนมัติทุกครั้งที่เกิด event บางอย่างใน Claude Code ผมใช้เพราะมัน
- ทำให้ workflow อัตโนมัติ — เช่นรัน Prettier ทุกครั้งที่ Claude แก้โค้ด
- ป้องกันความเสียหาย — block การแก้ไฟล์สำคัญ
- แจ้งเตือนเมื่องานยาวเสร็จ — Stop hook ส่ง notification
Event ที่ดักได้
| Hook | ตอน | ใช้ทำ |
|---|---|---|
| UserPromptSubmit | คนกด Enter ส่ง prompt | Validate input, log |
| PreToolUse | ก่อน Claude เรียก tool | Block, validate, modify |
| PostToolUse | หลัง tool ทำงาน | Format, lint, notify |
| Stop | Session จบ | Export, summary, notification |
| Notification | Claude ส่ง notification | Custom alert |
Setup hook พื้นฐาน
แก้ ~/.claude/settings.json
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "npx prettier --write $CLAUDE_FILE_PATHS"
}
]
}
]
}
}
แปลความ — ทุกครั้งที่ Claude ใช้ Edit/Write รัน Prettier กับไฟล์ที่แก้อัตโนมัติ
Hook ที่ผมใช้จริงในบริษัท
1) Auto format โค้ดหลังแก้
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "npx prettier --write $CLAUDE_FILE_PATHS" },
{ "type": "command", "command": "npx eslint --fix $CLAUDE_FILE_PATHS" }
]
}
]
}
}
2) Block ไม่ให้แก้ .env
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "if [[ \"$CLAUDE_FILE_PATHS\" =~ \\.env ]]; then echo 'BLOCKED — ห้ามแก้ .env' && exit 2; fi"
}
]
}
]
}
}
ผลคือ Claude ขอแก้ .env hook block ทันที กันเผลอลืม
3) Notification เมื่อ session จบ
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "osascript -e 'display notification \"งาน Claude เสร็จแล้ว\" with title \"เสร็จ\"'"
}
]
}
]
}
}
Mac ใช้ osascript / Windows ใช้ powershell BurntToastNotification
ผมใช้ตอนสั่งงานยาวๆ แล้วไปทำอย่างอื่น พอเสร็จเครื่องดังเตือน
4) Backup ก่อนลบไฟล์
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash(rm:*)",
"hooks": [
{
"type": "command",
"command": "mkdir -p /tmp/claude-backup && cp -r $CLAUDE_FILE_PATHS /tmp/claude-backup/ 2>/dev/null; true"
}
]
}
]
}
}
ก่อน Claude รัน rm คัดลอกไฟล์ไปที่ /tmp/claude-backup/ ก่อน ถ้าลบผิดเอามาคืนได้
Environment Variables ที่ใช้ใน hook
| Var | ค่า |
|---|---|
$CLAUDE_FILE_PATHS | ไฟล์ที่ Edit คั่นด้วย space |
$CLAUDE_SESSION_ID | Session ID |
$CLAUDE_TOOL_NAME | ชื่อ tool ที่ถูกเรียก |
$CLAUDE_TOOL_INPUT | Args ที่ส่งให้ tool (JSON) |
$CLAUDE_PROJECT_DIR | Project root |
Exit code ที่สำคัญ
- Exit 0 ผ่าน Claude ทำต่อได้
- Exit 1 fail แต่ไม่ block แค่ warning
- Exit 2 block Claude ไม่ทำ action นั้น
ใช้ exit 2 เป็นเครื่องมือ enforcement สำคัญ
Pro Tip — Hook สำหรับ policy
ตัวอย่าง — ห้ามมี console.log ใน src/
.claude/hooks/no-console-log.sh
#!/bin/bash
if [[ "$CLAUDE_FILE_PATHS" =~ src/ ]]; then
if grep -l "console\.log" $CLAUDE_FILE_PATHS 2>/dev/null; then
echo "BLOCKED — ห้ามมี console.log ใน src/"
exit 2
fi
fi
exit 0
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": "bash .claude/hooks/no-console-log.sh" }
]
}
]
}
}
ของที่ต้องระวัง
- Hook รันบนเครื่องคุณ — อย่าใส่ command ที่ดึงจากเน็ตมารัน อย่า trust hook ที่คน upload มา ตรวจก่อน
- Hook ช้า = Claude ช้า — ถ้า hook รันหลายวินาที Claude ค้างรอ ใช้
&ให้ run background ถ้าไม่ต้อง block - Hook error อาจทำให้ Claude ค้าง — ทดสอบก่อนใช้จริง ตรวจ exit code ให้ถูก
ลองทำดู: ภารกิจ: ตั้ง hook แรก
บทนี้มีประโยชน์กับคุณมั้ยครับ?
ผมอ่าน feedback เองทุกอันแล้วเอาไปปรับเนื้อหา