Home   git  

How to use husky

Husky is a tool used in web development workflows, particularly in projects that utilize Git for version control. It is a Git hooks manager that allows developers to automate tasks or enforce policies based on specific Git events or actions. Here’s an overview of what Husky does and how it is typically used:

Key Features of Husky:

  1. Git Hooks Management:

    • Pre-commit Hooks: Executes scripts or commands before a commit is made, allowing developers to perform tasks like code linting, formatting checks, or running tests.
    • Pre-push Hooks: Runs scripts or commands before a push operation to enforce rules such as running integration tests or ensuring code quality standards.
    • Commit Message Hooks: Enforces rules or conventions for commit messages, such as ensuring messages follow a specific format or include issue references.
  2. Configuration via package.json:

    • Husky is configured through the package.json file of a project, where developers define which scripts or commands should be executed for each Git hook event.
    • This makes it easy to manage and share configuration across team members working on the same project.
  3. Integration with Linters, Test Runners, and CI/CD Pipelines:

    • Husky integrates seamlessly with popular tools and scripts used in development workflows, such as ESLint for code linting, Jest for testing, or custom scripts for environment validation.
    • It helps enforce coding standards, maintain consistency, and prevent errors from being committed or pushed to the repository.

Example Use Cases:

Benefits of Using Husky:

Husky Example

Here's a simple example of how you can use Husky to enforce code linting before allowing a commit using ESLint as an example:

Step-by-Step Example with Husky and ESLint

  1. Setup ESLint and Husky:

    First, ensure you have ESLint installed in your project. If not, you can install it using npm:

    npm install eslint --save-dev
    

    Next, install Husky and lint-staged, which is used to run tasks on staged files:

    npm install husky lint-staged --save-dev
    
  2. Configure ESLint:

    Set up ESLint configuration (.eslintrc.json or .eslintrc.js) in your project. Here's a basic example:

    {
      "extends": "eslint:recommended",
      "rules": {
        "semi": ["error", "always"],
        "quotes": ["error", "double"]
        // Add more rules as needed
      }
    }
    
  3. Configure Husky:

    Update your package.json to include Husky configuration and define a pre-commit hook to run ESLint on staged files:

    {
      "scripts": {
        "lint": "eslint --ext .js,.jsx .",
        "lint-staged": "lint-staged"
      },
      "lint-staged": {
        "*.js": [
          "eslint --fix",
          "git add"
        ]
      },
      "husky": {
        "hooks": {
          "pre-commit": "lint-staged"
        }
      }
    }
    
    • Explanation:
      • scripts.lint: Defines a script to run ESLint on all .js and .jsx files in the project.
      • lint-staged section: Configures lint-staged to run ESLint with --fix on staged files (files that are about to be committed).
      • husky.hooks.pre-commit: Specifies that lint-staged should be executed before each commit.
  4. Usage:

    Now, whenever you attempt to commit changes, Husky will automatically run ESLint on staged files (those that are part of the commit). If ESLint detects any linting issues, the commit will be prevented until all issues are fixed.

Benefits:

By using Husky with ESLint (or other tools like Prettier for formatting), teams can maintain code quality, adhere to coding standards, and prevent errors from being committed into the repository, thereby improving overall codebase quality and developer productivity.

Published on: Jul 08, 2024, 08:33 AM  
 

Comments

Add your comment