,

Setting Up Husky, Commit Lint, and Code Standards 🧑‍💻🔧

Posted by


Husky, Commit Lint, and Code Standards Setup are tools that can help you maintain a clean and structured codebase in your projects. In this tutorial, we will guide you through the process of setting up these tools in your project to enforce code standards and ensure consistent commit messages.

Step 1: Install Husky

Husky is a tool that allows you to run scripts before committing or pushing code to your repository. To install Husky, run the following command in your project directory:

npm install husky –save-dev

This command will install Husky as a dev dependency in your project.

Step 2: Configure Husky

After installing Husky, you need to configure it to run specific scripts before committing or pushing code. To do this, create a file named "husky.config.js" in the root of your project directory and add the following configuration:

module.exports = {
  hooks: {
    'pre-commit': 'npm run lint',
    'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
  },
};

In this configuration, we are running the "lint" script before committing code and using Commit Lint to enforce commit message standards.

Step 3: Install Commit Lint

Commit Lint is a tool that helps you enforce consistent commit messages in your project. To install Commit Lint, run the following command in your project directory:

npm install @commitlint/config-conventional @commitlint/cli –save-dev

This command will install Commit Lint and the Conventional Commit configuration as dev dependencies in your project.

Step 4: Configure Commit Lint

After installing Commit Lint, you need to configure it to enforce specific commit message standards. Create a file named "commitlint.config.js" in the root of your project directory and add the following configuration:

module.exports = {extends: ['@commitlint/config-conventional']};

This configuration extends the Conventional Commit configuration, which enforces commit message standards such as using a specific format for commit messages.

Step 5: Add Linting Rules

To enforce code standards in your project, you should also set up linting rules using a tool like ESLint or Prettier. Install ESLint or Prettier and configure it in your project according to your preferred coding standards.

Step 6: Test your Setup

To test your setup, try committing code to your repository with a commit message that does not follow the Conventional Commit format. Husky should prevent you from committing code with an error message indicating that the commit message does not pass the Commit Lint standards.

Congratulations! You have successfully set up Husky, Commit Lint, and Code Standards in your project. These tools will help you maintain a clean and structured codebase, enforce commit message standards, and ensure consistent coding practices in your projects. Happy coding!