DEV Community

Cover image for Integrate Commitizen with your node.js project
Brayan Arrieta
Brayan Arrieta

Posted on • Updated on

Integrate Commitizen with your node.js project

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
Enter fullscreen mode Exit fullscreen mode

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"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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

{
  "path": "cz-conventional-changelog"
}
Enter fullscreen mode Exit fullscreen mode

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

{
  ...
  "scripts": {
    "commit": "cz"
  }
}
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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",
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Was created a new post for the automatic changelogs or release notes process for your project. How to Automatically Generate Changelog for your node.js projects

References

Top comments (0)