Husky
Automatically run Ultracite on your staged Git files with Husky
Ultracite provides seamless integration with Husky, a popular tool for managing Git hooks. This integration automatically sets up pre-commit hooks to ensure your code is properly formatted before every commit.
What is Husky?
Husky is a tool that makes it easy to use Git hooks in your project. Git hooks are scripts that run automatically at certain points in the Git workflow, such as before committing or pushing code. Husky simplifies the process of setting up and managing these hooks.
How Ultracite Uses Husky
When you initialize Ultracite in your project, it can automatically configure Husky to run code formatting before each commit. This ensures that:
- All committed code follows consistent formatting standards
- Code quality checks are enforced automatically
- Team members don't need to remember to format code manually
- Your repository maintains clean, consistent code style
Pre-commit Hook Behavior
Ultracite's Husky integration creates a pre-commit hook that runs the following command:
This command:
- Runs Biome's linter and formatter on your codebase
- Automatically fixes formatting issues where possible
- Ensures all staged files meet your project's code standards
- Prevents commits that would introduce formatting inconsistencies
Automatic Setup
During the initialization process (ultracite init
), if you select the Husky option, Ultracite:
- Installs Husky: Adds Husky as a development dependency to your project
- Creates Hook File: Sets up the
.husky/pre-commit
file with the formatting command - Handles Existing Setups: If you already have Husky configured, it updates your existing pre-commit hook rather than overwriting it
Integration Details
The Husky integration includes several smart features:
Dependency Management
- Automatically installs Husky as a dev dependency using your project's package manager
- Uses the appropriate package manager command (
npm install -D
,yarn add -D
,pnpm add -D
, etc.)
Hook Management
- New Projects: Creates a new
.husky/pre-commit
file with the Ultracite format command - Existing Projects: Appends the Ultracite command to your existing pre-commit hook, preserving other hooks you may have configured
File Structure
The integration creates or modifies:
Benefits
Consistent Code Style
Every commit automatically follows your project's formatting standards, eliminating style debates and inconsistencies.
Automated Quality Control
Catch formatting issues before they enter your repository, maintaining clean commit history.
Team Collaboration
All team members automatically follow the same formatting standards without manual intervention.
CI/CD Optimization
Reduce CI/CD pipeline failures due to formatting issues, as code is pre-formatted locally.
Workflow Integration
With Husky integration enabled, your typical Git workflow becomes:
- Make changes to your code files
- Stage changes with
git add
- Commit changes with
git commit
- Pre-commit hook runs automatically, formatting staged files
- Commit proceeds with properly formatted code
If the formatter makes changes, you'll need to stage and commit those changes as well.
Customization
While Ultracite sets up sensible defaults, you can customize the behavior:
- Modify
.husky/pre-commit
to add additional commands - Configure Biome settings to adjust formatting rules
- Add other pre-commit checks alongside Ultracite's formatting
Troubleshooting
Hook Not Running
If the pre-commit hook isn't executing:
- Ensure Husky is installed:
npm ls husky
- Check that
.husky/pre-commit
exists and is executable - Verify Git hooks are enabled in your repository
Permission Issues
On Unix-like systems, ensure the hook file is executable:
Bypassing Hooks
In rare cases where you need to skip the pre-commit hook:
Use this sparingly, as it bypasses the automated formatting that keeps your codebase consistent.