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:
-
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.
-
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.
- Husky is configured through the
-
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:
- Code Quality: Run ESLint or Prettier before allowing a commit to ensure code formatting and style consistency.
- Testing: Execute unit tests or integration tests before pushing changes to ensure that new code doesn't break existing functionality.
- Commit Message Conventions: Enforce rules for commit messages, such as requiring them to start with a specific prefix or follow a certain structure.
Benefits of Using Husky:
- Automated Workflow: Automates repetitive tasks and checks during the development process, reducing manual effort and ensuring consistent quality.
- Early Feedback: Provides immediate feedback to developers by catching issues early in the development lifecycle, before changes are integrated into the main codebase.
- Enforcement of Standards: Helps enforce coding standards, testing practices, and project-specific policies across the team.
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
-
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
-
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 } }
-
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: Configureslint-staged
to run ESLint with--fix
on staged files (files that are about to be committed).husky.hooks.pre-commit
: Specifies thatlint-staged
should be executed before each commit.
- Explanation:
-
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:
- Automated Code Quality: Ensures that only code that passes ESLint rules is committed.
- Consistency: Enforces coding standards across the team.
- Immediate Feedback: Provides instant feedback to developers during the commit process.
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.