How to improve your commit messages with Commitizen for your node.js projects

How to improve your commit messages with Commitizen for your node.js projects

When are working on several projects a common problem could be the different conventions or standardization with the commits, to that kind of problem can be used Commitizen which will create an easy and good integration with your projects.

A benefit of the standardization is related to generate an automatic changelog or release notes for the project.

When you commit with Commitizen, you'll be prompted to fill out any required commit fields at commit time.

Setup Commitizen

First of all, we need to install dependencies in the project.

npx commitizen init cz-conventional-changelog --save-dev --save-exact

The above command does three things for you:

  1. Installs the cz-conventional-changelog adapter npm module
  2. Saves it to package.json's dependencies or devDependenciesAdds
  3. The config.commitizen key to the root of your package.json as shown here
{
  ...
  "config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
  }
}

Alternatively, commitizen configs may be added to a .czrc file:

{
  "path": "cz-conventional-changelog"
}

After that, you can add some new npm run scripts in your package.json

{
  ...
  "scripts": {
    "commit": "cz"
  }
}

This will be more convenient for your users because then if they want to do a commit, all they need to do is run npm run commit and they will get the prompts needed to start a commit!

Commitizen Questions

  1. Select the type of change that you're committing: feat: A new feature? (For more information see the Commitizen type changes section)
  2. What is the scope of this change (e.g. component or filename): (press enter to skip)
  3. Write a short, imperative tense description of the change (max 82 chars):
  4. Provide a longer description of the change: (press enter to skip):
  5. Are there any breaking changes? yes/no
  6. Does this change affect any open issues? yes/no

Commitizen type changes

TypeDescription
featA new feature
fixA bug fix
docsDocumentation only changes
styleChanges that do not affect the meaning of the code
refactorA code change that neither fixes a bug nor adds a feature
perfA code change that improves performance
testAdding missing tests or correcting existing tests
buildChanges that affect the build system or external dependencies
ciChanges to our CI configuration files and scripts
choreOther changes that don't modify src or test files
revertReverts a previous commit

Running Commitizen on git commit (Optional)

Traditional git hooks

Update .git/hooks/prepare-commit-msg with the following code:

exec < /dev/tty && node_modules/.bin/cz --hook || true

Husky

For husky users, add the following configuration to the project's package.json:

{
  ...
  "husky": {
    "hooks": {
      "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
    }
  }
}

Conclusion

As you see Commitizen helps us to standardized the commit messages for our node.js projects. Also related to this topic if you are interested was created a post for the automatic changelogs or release notes process for your project. How to Automatically Generate Changelog for your node.js projects

I hope you find this article really helpful. I will be updating this post based on your comments and recommendations so let me know in any case. Thanks! 👍

References