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:
- Installs the cz-conventional-changelog adapter npm module
- Saves it to package.json's dependencies or devDependenciesAdds
- 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
- Select the type of change that you're committing: feat: A new feature? (For more information see the Commitizen type changes section)
- What is the scope of this change (e.g. component or filename): (press enter to skip)
- Write a short, imperative tense description of the change (max 82 chars):
- Provide a longer description of the change: (press enter to skip):
- Are there any breaking changes? yes/no
- Does this change affect any open issues? yes/no
Commitizen type changes
Type | Description |
feat | A new feature |
fix | A bug fix |
docs | Documentation only changes |
style | Changes that do not affect the meaning of the code |
refactor | A code change that neither fixes a bug nor adds a feature |
perf | A code change that improves performance |
test | Adding missing tests or correcting existing tests |
build | Changes that affect the build system or external dependencies |
ci | Changes to our CI configuration files and scripts |
chore | Other changes that don't modify src or test files |
revert | Reverts 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! 👍