Skip to content
On this page

Husky

Modern native Git hooks made easy 🐶

Installatie

Husky kan je globaal installeren via NPM:

bash
npm install husky -D

WARNING

Wanneer je Yarn 2 gebruikt, is de installatie en setup van Husky anders: info

Configuratie

Om Husky te beginnen gebruiken, moet je een script toevoegen aan je package.json:

bash
npm set-script prepare "husky install"
npm run prepare

Dit zal Husky installeren en een .husky folder aanmaken in je project waar de hooks in terecht komen.

Gebruik

Gebruik volgend command om nieuwe hooks toe te voegen:

bash
npx husky add .husky/pre-commit "npm test"

Deze hooks zijn (standaard) terug te vinden in .husky, daar kan je ze dus ook aanpassen.

.husky/pre-commit

sh
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged

# PHPSTAN
# 0 - basic checks, unknown classes, unknown functions, unknown methods called on $this, wrong number of arguments passed to those methods and functions, always undefined variables
# 1 - possibly undefined variables, unknown magic methods and properties on classes with __call and __get
# 2 - unknown methods checked on all expressions (not just $this), validating PHPDocs
# 3 - return types, types assigned to properties
# 4 - basic dead code checking - always false instanceof and other type checks, dead else branches, unreachable code after return; etc.
# 5 - checking types of arguments passed to methods and functions
# 6 - report missing typehints
# 7 - report partially wrong union types - if you call a method that only exists on some types in a union type, level 7 starts to report that; other possibly incorrect situations
# 8 - report calling methods and accessing properties on nullable types
vendor/bin/phpstan analyse -l 5 src

Hooks

  • pre-commit: Eerste hook die wordt aangeroepen. Als deze non-zero exit dan wordt de commit afgeblazen.
  • prepare-commit-msg: Kan gebruikt worden om de commit messages aan te passen of uit te breiden.
  • commit-msg: Kan gebruikt worden om te commit message na te kijken. Als deze non-zero exit dan wordt de commit afgeblazen.
  • post-commit: De laatste hook die wordt aangeroepen in de lifecycle. Kan gebruikt worden om notifications en dergelijke uit te sturen na een commit.

Dit zijn de meestgebruikte git hooks voor onze use case, maar er zijn nog andere hooks die je kan gebruiken indien nodig .