How it works
The hook:- Triggers on file edits: Runs after Write or Edit tool calls
- Detects file type: Checks file extension to determine formatter
- Runs appropriate formatter: Executes prettier, black, gofmt, rustfmt, etc.
- Provides feedback: Reports formatting results to the user
- Handles errors gracefully: Continues even if formatting fails
Prerequisites
Install formatters for your language stack:Basic setup
Single language project
For a JavaScript/TypeScript project, add this to your.factory/hooks.json:
Multi-language project
For projects with multiple languages, use a script to handle different file types. Create.factory/hooks/format.sh:
.factory/hooks.json:
Advanced configurations
Format with custom config
Use project-specific prettier config:Format with linting
Combine formatting with linting fixes. Create.factory/hooks/format-and-lint.sh:
.factory/hooks.json:
Conditional formatting
Only format files in specific directories. Create.factory/hooks/format-src-only.sh:
.factory/hooks.json:
Real-world examples
Example 1: React component formatting
- Before Hook
- After Hook
Example 2: Python import sorting
- Before Hook
- After Hook
Best practices
1
Start with read-only mode
Test your formatter configuration manually first:
2
Use consistent config files
Ensure formatter configs are committed:
3
Set appropriate timeouts
Large files may need more time:
4
Handle formatter errors gracefully
Don’t block Droid if formatting fails:
5
Consider Git hooks integration
Combine with pre-commit hooks for consistency:
Troubleshooting
Formatter not found
Problem:command not found error
Solution: Install the formatter globally or use npx/project binaries:
Formatting breaks code
Problem: Formatter introduces syntax errors Solution: Add file validation after formatting:Hook runs too slowly
Problem: Formatting takes too long Solution: Only format changed files, use faster formatters:Conflicts with editor formatting
Problem: Editor and hook format differently Solution: Use the same config for both:See also
- Hooks reference - Complete hooks API documentation
- Get started with hooks - Basic hooks introduction
- Code validation hooks - Enforce code standards
- Git workflow hooks - Integrate with Git
