What is Conventional Commit?
A specification for adding human and machine readable meaning to commit messages
In this tutorial, I will guide you on configuring conventional commit for a Javascript/TypeScript/NodeJS project.
Tutorial
This tutorial uses pnpm.
Configure husky
- Install dependency packages
shell
pnpm add -D husky- Active husky hook manually
shell
pnpm exec husky install- Active husky hook automatically on first time setting up project in the future
shell
pnpm pkg set scripts.prepare="husky install"Note:
- You need to add the script
preparemanually if it exists to avoid overwriting. preparescript is incompatible withyarn berry(v2+) at this moment.
Configure commitlint
- Install dependency packages
shell
pnpm add -D @commitlint/config-conventional @commitlint/cli- Configure
commitlintto use conventional config
shell
echo '{ "extends": [ "@commitlint/config-conventional" ] }' > .commitlintrc.jsonor
shell
echo "module.exports = { extends: [ '@commitlint/config-conventional' ] }" > commitlint.config.js- Add husky hook to validate commit message on committing
shell
pnpm exec husky add .husky/commit-msg 'pnpm exec commitlint --edit ${1}'Configure commitizen
- Install dependency packages
shell
pnpm add -D commitizen cz-conventional-changelog- Init
commitizenconfig
shell
echo '{ "path": "./node_modules/cz-conventional-changelog" }' > .czrc- Add husky hook to trigger commitizen automatically on running
git commit
shell
pnpm exec husky add .husky/prepare-commit-msg 'exec < /dev/tty && [ -z "$(cat ${1})" ] && pnpm exec cz --hook || true'Advanced script for prepare-commit-msg hook
shell
if [ -t 0 ];
then
# running via terminal
exec < /dev/tty && [ -z "$(cat ${1})" ] && pnpm exec cz --hook || true
else
# running via GUI
[ -z "$(cat ${1})" ] && pnpm exec cz --hook || true
fi- Add badge (optional) to
README.md
markdown
[](http://commitizen.github.io/cz-cli/)


