Chapter 12: Hooks
Hook Mechanism
Hooks allow you to run custom scripts before and after Claude Code performs specific operations.
Lifecycle Hooks
12.1 钩子机制
json
{
"hooks": {
"pre-command": "./scripts/pre-command.sh",
"post-command": "./scripts/post-command.sh",
"pre-commit": "./scripts/lint.sh",
"post-commit": "./scripts/notify.sh"
}
}Custom Hooks
Example: Pre-commit Hook
12.2 自定义钩子
bash
#!/bin/bash
# .claude-code/hooks/pre-commit.sh
echo "Running pre-commit checks..."
# Run linter
npm run lint || exit 1
# Run tests
npm test || exit 1
echo "✓ Pre-commit checks passed"Summary
In this chapter, we learned:
- ✅ Hook mechanism concepts
- ✅ Lifecycle hook configuration
- ✅ Custom hook examples
Next Step: Chapter 13 is the CLI Reference!