DEV Community

Shashank Mishra
Shashank Mishra

Posted on

Part 2 : Configure Commitizen using Husky tool for automate version and structured commit message

In this part, I’ll walk you through configure Commitizen using Husky tool for automate version (major, minor, patch) and structured git commit message. We will use this automate generated version in upcoming semantic release step pipeline

By the end of this guide, you’ll have a fully functioning git commit workflow to have structured commit message with automate version.

Step 1: Configure Husky

Navigate to your project root directory
1.a Add Husky to your project using npm or Yarn:
npm install --save-dev husky
1.b Run the following command to set up Husky in your project:
npx husky install
This command will create a .husky directory at root level of your project and will contain configuration files of husky hooks(pre-commit, pre-commit-message etc). We will add pre-commit-message command later on once we will be done with configuring Commitizen.

Step 2: Initialize and Configure Commitizen

2.a Install Commitizen as a development dependency:
npm install --save-dev commitizen
2.b Choose and Install an Adapter
Adapters define the format of commit messages. For example, use cz-conventional-changelog to follow the Conventional Commits specification:
npm install --save-dev cz-conventional-changelog
2.c Configure Commitizen
Tell Commitizen which adapter to use by adding the commitizenconfiguration to your package.json file:

{
  "commitizen": {
    "path": "./node_modules/cz-conventional-changelog"
  }
}
Enter fullscreen mode Exit fullscreen mode

2.d Add commitizen command to pre-commit-message hook of husky
Navigate to .husky directory and search for prepare-commit-msg file inside it and replace the content with below mention snippet.

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

exec < /dev/console && npx cz --hook || true
Enter fullscreen mode Exit fullscreen mode

Note : Git commands by default checks for .husky directory in your project's root level and run the hook accordingly.This will allow husky hook to execute the commitizen command and will open interactive session once you try to commit your changes.
Image description

Conclusion

By following these steps, you’ve successfully configured commitizen with husky in order to achieve structured commit message with automate semantic version control.
Stay tuned for more guides to help you optimize your pipelines and development practices. Happy coding! 🚀

Top comments (0)