Introduction
When creating Laravel+Livewire projects, especially for teams, pre-commit hooks come in handy by ensuring everyone on the team follows the same formatting standards and adheres to some level of code quality. In this blog-post we will use:
- husky: Sets up pre-commit hooks
- prettier: To format
*.css,*.jsand*.bladefiles. - @shufo/prettier-plugin-blade: A prettier plugin for formatting
*bladefiles - laravel/pint: to format PHP files like classes, traits, etc.
- larastan: does PHP static analysis.
Installation
Assuming you are creating a project from scratch, let us begin the set-up.
Create a new project, I will name mine
livewire-precommits-demo:$ composer create-project laravel/laravel livewire-precommits-demoOpen the project, initialize git and make the first commit:
$ cd livewire-precommits-demo $ git init $ git add -A $ git commit -m "First of many"Install
husky,prettier, and@shufo/prettier-plugin-bladeas dev dependencies:$ npm i -D husky prettier @shufo/prettier-plugin-bladeAdd Laravel Pint:
$ composer require laravel/pint --devTo test it out in action:
$ ./vendor/bin/pintInstall Larastan:
$ composer require larastan/larastan:^2.0 --devCreate a
phpstan.neonfile in the root of the project with this:includes: - vendor/larastan/larastan/extension.neon parameters: paths: - app/ # Level 9 is the highest level level: 5 # ignoreErrors: # - '#PHPDoc tag @var#' # # excludePaths: # - ./*/*/FileToBeExcluded.php # # checkMissingIterableValueType: falseTo test if it works:
$ ./vendor/bin/phpstan analyseWe have installed everything we need, now we can create a commit to track these changes:
$ git add -A && git commit -m "Added linting and formatting packages"
Set Up Pre-Commit Hook
We have all the packages needed for static analysis and code formatting so now let us create a pre-commit hook that runs them before every commit:
First we should install husky’s Git hooks
$ npx husky installCreate a file under the
.huskydirectory calledpre-commit, in full (.husky/pre-commit), and add the following content:#!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" # Format resources # https://stackoverflow.com/a/6879568 # https://stackoverflow.com/a/1587952 ./node_modules/.bin/prettier --ignore-unknown --write $(git diff --name-only --diff-filter=ACMR HEAD -- '*.blade.php') ./node_modules/.bin/prettier resources/**/*.js resources/**/*.css --write # Format php classes ./vendor/bin/pint && git add -A # Do static analysis ./vendor/bin/phpstan analyse --memory-limit=2GLet us break down this command:
/node_modules/.bin/prettier --ignore-unknown --write $(git diff --name-only --diff-filter=ACMR HEAD -- '*.blade.php')--ignore-unknownprevents the command from returning a non-zero result if no match is found in our filter--writetells prettier to write the formatting changes to the respective files--diff-filter=ACMR HEADmatches files that have either been Added, Copied, Modified, or Renamed.HEADadds to this filter by matching files that have gone through either of the aforementioned changes, whether they have been stagged for commit or not. If this is not the desired behaviour this resource should come in handy
Make the
.husky/pre-commitexecutable:$ chmod +x .husky/pre-commitNow when we do a commit, the pre-commit hook should run
Commit our changes:
$ git add -A $ git commit -m "Added pre-commit hook"Congratulations! Now linting and static analysis is done before every commit. There’s one more thing we can do to help new team members install the pre-commit hook:
Update
package.jsonand the following script. It runs right after one runsnpm install:{ "scripts": { ... "prepare": "husky install && git add .husky/pre-commit" } }Now an
npm installshould trigger the installation of the pre-commit hooks, or one can just runnpm run prepare.