Skip to main content

Husky

Husky is a tool used to automate Git hooks in a project. Git hooks are scripts that run automatically before or after certain Git commands, such as commit or push. They allow you to run custom code to enforce coding standards, check for security vulnerabilities, or run tests before committing or pushing code.

Husky simplifies the process of creating and managing Git hooks by providing a way to define them in your project's package.json file. Once you've defined your hooks, Husky will automatically run them whenever the corresponding Git command is executed.

For example, you can define a pre-commit hook in Husky to run ESLint on your code before it is committed. Here's how you would define the hook in your package.json file:

{
"scripts": {
"lint": "eslint ."
},
"husky": {
"hooks": {
"pre-commit": "yarn lint"
}
}
}

In this example, whenever a user runs git commit, Husky will automatically run the yarn lint command, which will in turn run ESLint on the project.

Husky also supports other Git hooks, such as pre-push, commit-msg, and post-merge. By using Husky to automate your Git hooks, you can ensure that your code is always tested, linted, and analyzed before it is committed or pushed. This can help catch bugs and prevent issues before they make it into production.

Overall, Husky is a powerful tool that can help improve the quality of your code and streamline your development process by automating Git hooks.

tip

At Figuro, it is required that every project implements a lint script in their package.json file to ensure that the linter is executed on every commit. This helps maintain consistency and catch potential issues early on in the development process. Husky can be used to automate this process by running the lint script as a pre-commit hook.

References

  • Husky GitHub Repository: The official repository of Husky, where you can find the documentation, code, and releases.
  • Husky Documentation: The official documentation of Husky, which provides a detailed explanation of how to use it and its features.
  • Husky NPM Package: The official package of Husky in the NPM registry, where you can download and install it in your projects.