Skip to content

Instantly share code, notes, and snippets.

@bewithdhanu
Last active July 3, 2025 09:47
Show Gist options
  • Save bewithdhanu/e3552c97be0058a4ee8afcb640f492c3 to your computer and use it in GitHub Desktop.
Save bewithdhanu/e3552c97be0058a4ee8afcb640f492c3 to your computer and use it in GitHub Desktop.
Setting Up Husky with Laravel Pint for Automated Code Formatting

Setting Up Husky with Laravel Pint for Automated Code Formatting

This guide will help you integrate Husky (Git hooks manager) with Laravel Pint (PHP code style fixer) to automatically format your PHP code before every commit.

Prerequisites

  • Node.js and npm installed
  • Composer installed
  • Laravel project set up

πŸ“‹ Prerequisites

Before starting, ensure you have:

  • PHP 8.1+ with Composer installed
  • Node.js 16+ with npm installed
  • Git configured on your machine
  • Laravel project already cloned/downloaded

πŸš€ Quick Setup (5 minutes)

Step 1: Install Dependencies

Make sure your project has these files already created/copied

.husky/commit-msg 
.husky/pre-commit 
.husky/pre-push 
commands/pre-commit 
commands/pre-push 
.editorconfig
.eslintignore
.eslintrc.js
β€’gitignore
β€’prettierignore 
.prettierrc 
commitlint.config.cjs
DEVELOPMENT-WORKFLOW.md 
package-lock.json 
package.json
pint.json
SETUP.md

Make sure you have laravle/plnt exist in composer.json, if not exist run this command

composer require laravel/pint --dev

Check package.json for husky, lint-staged prettier, eslint, eslint-plugin-vue@next, eslint-config-airbnb-base, @commitlint/{cli,config-conventional}, if not exist then run these commands selectively

npm install --save-dev husky lint-staged prettier
npm install --save-dev eslint eslint-plugin-vue@next
npx install-peerdeps --dev eslint-config-airbnb-base
npm install --save-dev @commitlint/{cli,config-conventional}

Run these commands to make sure all dependencies in PHP, Node.js are installed

# Install PHP dependencies (Laravel Pint is already included)
composer install

# Install Node.js dependencies (ESLint, Prettier, Husky, etc.)
npm install

Step 2: Initialize Git Hooks

# Setup Husky git hooks
npx husky init

Step 3: Verify Installation

# Check if hooks are installed
ls -la .husky/

# Should show:
# pre-commit
# pre-push  
# commit-msg

βœ… Test Your Setup

Test PHP Formatting

# Check PHP code style (dry run)
npm run pint:check

# Fix PHP code style
npm run pint

Test JavaScript Linting

# Check JavaScript files
npm run lint:check

# Fix JavaScript files  
npm run lint

Test Git Hooks

# Create a test branch (any name works now)
git checkout -b test-setup

# Try to commit (hooks will run automatically)
git add .
git commit -m "feat: test development workflow setup"

# If successful, you'll see:
# βœ… Running pre-commit checks...
# βœ… Branch name 'test-setup' is valid
# βœ… Pre-commit checks passed!
# βœ… [lint-staged output]

🎯 What You Should See

βœ… Successful Hook Run

βœ… Running pre-commit checks...
βœ… Branch name 'test-setup' is valid  
βœ… Pre-commit checks passed!
βœ… Running lint-staged...
βœ… Laravel Pint formatting files...
βœ… ESLint checking files...
βœ… Prettier formatting files...
βœ… Commit successful!

βœ… What You'll See Now

All branches are allowed, so you won't see branch-related errors anymore!

πŸ› οΈ Available Commands

After setup, you can use these commands:

# PHP (Laravel Pint)
npm run pint           # Fix PHP code style
npm run pint:check     # Check PHP code style (dry run)

# JavaScript (ESLint + Prettier)  
npm run lint           # Fix JavaScript issues
npm run lint:check     # Check JavaScript (dry run)
npm run format         # Format with Prettier
npm run format:check   # Check Prettier formatting

# Git Hooks
npx husky init        # Reinstall git hooks

🎯 Branch Naming (Optional)

For better organization, consider using this pattern:

type/description-with-hyphens

Suggested examples:

  • feature/user-authentication
  • fix/login-validation-bug
  • chore/update-dependencies
  • docs/setup-instructions

Note: Branch naming is now optional - you can use any branch name you prefer.

πŸ†˜ Troubleshooting

Git Hooks Not Running

# Reinstall hooks
rm -rf .husky
npx husky init
chmod +x .husky/pre-commit .husky/pre-push .husky/commit-msg

"Command not found" Errors

# Make sure dependencies are installed
npm install
composer install

# Make sure hooks are executable
chmod +x .husky/pre-commit .husky/pre-push .husky/commit-msg
chmod +x commands/pre-commit commands/pre-push

PHP Pint Issues

# Check if vendor/bin/pint exists
ls -la vendor/bin/pint

# If missing, reinstall Laravel Pint
composer require laravel/pint --dev

ESLint Issues

# Check ESLint configuration
npx eslint --print-config resources/js/app.js

# Reinstall if needed
npm install eslint eslint-config-airbnb-base eslint-plugin-import --save-dev

Commitlint Issues

# If you see module loading errors, ensure commitlint.config.cjs exists
ls -la commitlint.config.cjs

# Reinstall commitlint if needed
npm install @commitlint/cli @commitlint/config-conventional --save-dev

🎯 Branch Freedom

You can now commit and push directly to any branch including:

  • main
  • master
  • develop
  • staging
  • production

Note: While direct commits are allowed, consider using feature branches for better collaboration.

βœ‹ Emergency Bypass

Only use in emergencies:

git commit --no-verify -m "emergency fix"
git push --no-verify

⚠️ Warning: This bypasses all quality checks!

πŸ“š Next Steps

After successful setup:

  1. Read the full guide: Check DEVELOPMENT-WORKFLOW.md for detailed workflow information
  2. Create your first feature branch: git checkout -b feature/your-feature-name
  3. Make some changes and test the commit process
  4. Share with your team: Have them follow this same setup process

πŸŽ‰ Setup Complete!

Your development workflow is now active! Every commit will automatically:

  • βœ… Validate branch names
  • βœ… Format PHP code with Laravel Pint
  • βœ… Lint JavaScript with ESLint
  • βœ… Format code with Prettier
  • βœ… Validate commit messages

Happy coding with consistent, high-quality code! πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment