Notes of Nafi
  • Hello! 🖐
  • Cloud
    • AWS
    • GCP
    • Ansible
      • Desktop configuration
  • Web Dev
    • husky
    • Web Cache/HTTP Caching Headers
    • package manger
    • Webhook
    • ESLint
    • Back End
      • node.js
    • Front End
      • React
        • Lint
        • Next.js
      • Angular
      • Flutter
        • cubit
        • flutter
        • provider
      • Tailwind CSS
      • CSS
    • Lerna
    • nginx
  • Software Architecture
  • Startup
    • Legal Issues
  • Linux
    • Misc problems & solutions
    • CTF Tools
    • Network Tools
    • systemctl
    • terminal
      • dotfiles
      • tmux
      • zsh
      • command line tools
    • Git
      • merging strategy
      • git workflows
      • basics
  • Programming Language
    • JavaScript
      • misc
      • draft
    • C++
  • Google Docs
    • Spreadsheet
  • Design
  • Windows
    • Terminal
  • Similar websites
Powered by GitBook
On this page
  1. Web Dev

husky

Git hooks for Node.js

PreviousWeb DevNextWeb Cache/HTTP Caching Headers

Last updated 2 years ago

git provides a way to execute scripts before or after an event such as commit, push. These scripts are called git hooks. git hooks run locally on developers machine.

Every git repository by default has script for each hook you can bind to. These can be found at .git/hooks directory. We can update these scripts as per our need, rename and remove .sample from file name and git will execute them automatically.

Now, if we want to maintain some conventions or rules for a team through git hooks, we'll need to share it between the team.

For example, I want to make sure none of my team mates can commit without proper linting. Suppose I test my linting with yarn lint

So, I'll create a pre-commit hook which'll execute exactly before a commit and execute what it's told to. I'll add yarn lint to my pre-commit hook.

Now for the sharing part, comes in the play. We can add the hooks through husky and add it to the version control system. Voila! We're done

Installation

yarn add --dev husky
npm set-script prepare "husky install"  # adds prepare script on package.json
yarn prepare                            # prepares husky

Add a pre-commit hook

yarn husky add .husky/pre-commit "yarn lint"

Now, yarn lint will run every time on git commit. If it passes linting, then commit will be made otherwise not.

A list of all available git hooks & how it works with some projects and guides can be found .

Recourses

husky
here
Automatic Next.js Code Linting with ESLint & Husky Git Hooks
husky doc
githooks.com
githooks official documentation