Pre-commit Hooks Guide
This document explains the pre-commit hooks setup in the ERP-Unlocked monorepo and how to use them effectively.
Overview
Section titled “Overview”The repository uses Husky and lint-staged to automatically format and lint your code before each commit. This ensures consistent code quality and style across all contributors.
What Gets Checked
Section titled “What Gets Checked”When you run git commit, the following happens automatically:
1. Lint-Staged (Runs on Staged Files Only)
Section titled “1. Lint-Staged (Runs on Staged Files Only)”The pre-commit hook runs these tools on your staged files:
TypeScript/JavaScript/Astro Files (*.{ts,tsx,js,jsx,astro})
Section titled “TypeScript/JavaScript/Astro Files (*.{ts,tsx,js,jsx,astro})”- ESLint with auto-fix (
eslint --fix) - Prettier formatting (
prettier --write)
Python Files (*.py)
Section titled “Python Files (*.py)”- Ruff linting with auto-fix (
ruff check --fix) - Ruff formatting (
ruff format)
Markdown Files (*.md)
Section titled “Markdown Files (*.md)”- Prettier formatting (
prettier --write)
2. Changeset Check
Section titled “2. Changeset Check”If you modify files in apps/ or packages/, the hook checks if a changeset exists:
- ✅ Passes: If a changeset file exists in
.changeset/ - ⚠️ Warns: If no changeset found, prompts you to run
pnpm changeset - 🔄 Skips: Automatically skipped for merge commits
First Time Setup
Section titled “First Time Setup”When you clone the repository and run pnpm install, Husky is automatically initialized via the prepare script:
pnpm installThis installs all dependencies and sets up the Git hooks.
Manual Hook Installation
Section titled “Manual Hook Installation”If hooks aren’t working, you can manually trigger setup:
pnpm prepareNormal Workflow
Section titled “Normal Workflow”Just commit as usual! The hooks run automatically:
git add .git commit -m "feat: add new feature"The hooks will:
- Format and lint your staged files
- Check for changesets (if needed)
- Complete the commit if everything passes
Bypassing Hooks
Section titled “Bypassing Hooks”⚠️ Use sparingly! You can skip hooks if absolutely necessary:
git commit --no-verify -m "emergency hotfix"When to bypass:
- Emergency hotfixes that need immediate deployment
- Fixing broken CI/CD pipelines
- Addressing critical production issues
When NOT to bypass:
- Regular feature development
- Refactoring
- Documentation updates
Manual Formatting
Section titled “Manual Formatting”You can also run formatters manually:
Format Everything
Section titled “Format Everything”# TypeScript/JavaScript/Astro/Markdownpnpm format
# Pythonpnpm format:python
# Check formatting without changing filespnpm format:checkFormat Specific Files
Section titled “Format Specific Files”# Prettiernpx prettier --write path/to/file.ts
# Ruffuv run ruff format path/to/file.pyConfiguration Files
Section titled “Configuration Files”Prettier Configuration (.prettierrc)
Section titled “Prettier Configuration (.prettierrc)”{ "semi": true, "singleQuote": true, "tabWidth": 2, "trailingComma": "es5", "printWidth": 100, "bracketSpacing": true, "arrowParens": "avoid", "endOfLine": "lf"}Prettier Ignore (.prettierignore)
Section titled “Prettier Ignore (.prettierignore)”Excludes generated files, build outputs, dependencies, and lock files from formatting.
Lint-Staged Configuration (package.json)
Section titled “Lint-Staged Configuration (package.json)”{ "lint-staged": { "*.{ts,tsx,js,jsx,astro}": ["eslint --fix", "prettier --write"], "*.py": ["ruff check --fix", "ruff format"], "*.md": ["prettier --write"] }}Pre-commit YAML (.pre-commit-config.yaml)
Section titled “Pre-commit YAML (.pre-commit-config.yaml)”For developers using the pre-commit framework (optional):
repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files
- repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.8.4 hooks: - id: ruff args: [--fix] - id: ruff-formatTo use this:
# Install pre-commit (one-time)pip install pre-commit
# Install hookspre-commit install
# Run manually on all filespre-commit run --all-filesTroubleshooting
Section titled “Troubleshooting”Hooks Not Running
Section titled “Hooks Not Running”-
Check if hooks are installed:
Terminal window ls -la .husky/You should see a
pre-commitfile. -
Reinstall hooks:
Terminal window pnpm prepare -
Verify Git hooks directory:
Terminal window git config core.hooksPathShould output:
.husky
Formatting Conflicts
Section titled “Formatting Conflicts”If you get merge conflicts in formatted code:
- Accept the incoming changes
- Run the formatter:
Terminal window pnpm format# or for Pythonpnpm format:python - Stage and commit the formatted files
Linting Errors That Won’t Auto-Fix
Section titled “Linting Errors That Won’t Auto-Fix”Some linting errors require manual fixes. If the pre-commit hook fails:
- Review the error messages
- Fix the issues manually
- Stage the fixes:
git add . - Try committing again
Slow Pre-commit Hooks
Section titled “Slow Pre-commit Hooks”If hooks are slow on large commits:
- Commit in smaller chunks to reduce the number of files checked
- Use
--no-verifyonly for emergency situations
Changeset Warnings
Section titled “Changeset Warnings”If you get a changeset warning but don’t need one:
# For non-versioned changes (docs, configs, etc.)git commit --no-verify -m "docs: update README"
# For versioned changes, create a changeset:pnpm changesetgit add .changeset/*.mdgit commit -m "feat: add new feature"Best Practices
Section titled “Best Practices”For Contributors
Section titled “For Contributors”- Let the hooks run - They catch issues early
- Stage incrementally - Easier to review what’s changing
- Read error messages - They usually tell you exactly what to fix
- Format before reviewing - Makes diffs cleaner
For Code Reviewers
Section titled “For Code Reviewers”- Don’t worry about style - Hooks ensure consistency
- Focus on logic - Style is automated
- Check for
--no-verify- Flag commits that bypassed hooks
For Repository Maintainers
Section titled “For Repository Maintainers”- Update pre-commit config when tools change
- Keep linter versions in sync with CI/CD
- Document exceptions if certain files need different rules
CI/CD Integration
Section titled “CI/CD Integration”The same checks run in CI/CD pipelines:
# What runs in CIpnpm lint # Lints all apps and packagespnpm lint:python # Lints all Python codepnpm format:check # Verifies formattingpnpm test # Runs all testsThis ensures that even if someone bypasses local hooks, CI will catch issues.
Additional Resources
Section titled “Additional Resources”- Husky Documentation
- lint-staged Documentation
- Prettier Documentation
- Ruff Documentation
- ESLint Documentation
Getting Help
Section titled “Getting Help”If you encounter issues with pre-commit hooks:
- Check this documentation
- Review error messages carefully
- Ask in the team chat
- Create an issue in the repository
Remember: Pre-commit hooks are here to help, not hinder. They save time in code review and ensure consistent quality across the entire codebase.