This is the 10th day of my participation in Gwen Challenge

Code word is not easy, your praise is my motivation to continue to update the creation.

In a team, everyone’s Git commit information is different, and there is no mechanism to ensure standardization. How can standardization be achieved? You may be thinking of git hook mechanism, to write shell script to implement. Sure, JavaScript has a great tool for implementing this constraint: CommitLint. The next steps will show you how to use CommitLint step by step.

Depend on the installation

Install the following three libraries:

yarn add -D husky @commitlint/cli @commitlint/config-conventional
Copy the code

configuration

Open the package.json file and add the following code:

  "commitlint": {
    "extends": [
      "@commitlint/config-conventional"]},"husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}}Copy the code

At this point, if you submit code under the project that does not conform to the established style, the following message will be displayed

For CommitLint, the default configuration is generally sufficient. However, if you want to customize these rules without enabling the default rules, you can create a commitlint.config.js file in the project directory for more detailed configuration:

module.exports = {
  extends: [
    "@commitlint/config-conventional"].rules: {
    'type-enum': [2.'always'['upd'.'feat'.'fix'.'refactor'.'docs'.'chore'.'style'.'revert'
     ]],
    'type-case': [0].'type-empty': [0].'scope-empty': [0].'scope-case': [0].'subject-full-stop': [0.'never'].'subject-case': [0.'never'].'header-max-length': [0.'always'.72]}};Copy the code

Rule configuration description: The rule consists of name and configuration array, for example: ‘name: [0,’ always’, 72] “, the first level, in the array optional,1,2,0 0 to disable, 1 to warning, 2 for the error, the second for the application or not, the optional always | never, a third of the value of the rule.

For details, see the official documents

Use Commitizen to generate well-formed submission information

Conventional Commits are as follows:

# Note: The colon is preceded by a space, followed by? Indicates non-mandatory information
type(scope?) : subject body? footer?Copy the code

The prompt above mentions two points: Subject and type. How do I quickly generate a commitlint commit message? We can use the Commitizen library to install globally first:

yarn add global commitizen
Copy the code

To customize submission types and options in a project, you first need to install:

yarn add -D cz-customizable
Copy the code

Add the following code to package.json:

"config": {
    "commitizen": {
      "path": "node_modules/cz-customizable"}}Copy the code

Then add the.cz-config.js file in the project root directory

At this point, you can use git CZ instead of Git commit-m for code commit

Changelog is automatically generated

Once you have a well-formed Git message, you can use a script to generate a one-click change log for that version

Installation:

yarn global -D conventional-changelog-cli
Copy the code

Add the following code to your project’s package.json:

"scripts": {
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
  },
Copy the code

Then run the yarn Changelog command to generate changelog. md.

resources

The project code used in this article: try-Commitizen