Hooks and Automation
Hooks let you automate actions based on events. Run tasks automatically!
What Are Hooks?
Hooks are scripts that run when something happens:
- pre-commit: Before you commit code
- pre-push: Before pushing to GitHub
- post-save: After Claude saves a file
- on-error: When something fails
Setting Up Git Hooks
$ mkdir -p .husky
$ npx husky init
โ
Pre-commit Hook
$ cat .husky/pre-commit
#!/bin/sh
npm run lint
npm test
โ
Now tests run automatically before every commit!
โTip
Pre-commit hooks catch bugs before they enter your codebase.
Claude Code Hooks
Configure hooks in .claude/hooks.json:
$ cat .claude/hooks.json
{
"hooks": {
"after-edit": {
"description": "After Claude edits a file",
"actions": [
"Run prettier on the edited file",
"If it's a component, verify it exports correctly"
]
},
"before-commit": {
"description": "Before committing changes",
"actions": [
"Run npm test",
"Check for console.log statements",
"Verify no TODO comments in committed code"
]
}
}
}
โ
npm Scripts
Combine scripts in package.json:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"test": "jest",
"lint": "eslint .",
"format": "prettier --write .",
"check": "npm run lint && npm test",
"deploy": "npm run check && vercel --prod"
}
}
# One command to check everything
$ npm run check
# One command to deploy
$ npm run deploy
โ
๐ฆ๐บAussie Note
Automation is like having a robot assistant check your work. Set it up once, benefit forever! ๐ค
CI/CD with GitHub Actions
$ mkdir -p .github/workflows
$ cat .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm install
- run: npm test
โ
Now GitHub runs tests automatically on every push!
Having Claude Set Up Automation
> Set up GitHub Actions to run tests on every PR
Claude: I'll create a CI workflow:
๐ Create: .github/workflows/ci.yml
The workflow will:
1. Run on push and pull_request
2. Install dependencies
3. Run linting
4. Run tests
5. Report status on the PR
Proceed? [Y/n]
โ
Common Automation Patterns
| Trigger | Automation |
|---|
| PR opened | Run tests, check types |
| Push to main | Deploy to staging |
| Tag created | Deploy to production |
| Schedule (cron) | Run security scan |
| Issue created | Label by keywords |
โ Warning
Start with simple automation. Add more as you get comfortable!
Key Takeaways
- Hooks automate tasks on events
- Use pre-commit hooks to catch bugs early
- npm scripts combine common commands
- GitHub Actions provide CI/CD
- Start simple, expand over time