preface

  • Git as an essential tool for developers, code submission is also a very frequent operation in daily life, so it is definitely necessary to standardize the submission information.
  • If you manually submit the code to the specification, it can be a little cumbersome. What to do? It is better to use a tool.
  • Commitizen is an appropriate tool for git commit specification information. Commitizen helps teams standardize commit format information for traceability purposes.
  • After a specification commit with CommItizen, we can automate versioning with standard-version, such as updating Changelog.md.

Committezen Specifies specification submission information

When you use Committezen, you will be prompted to fill in all required commit fields at Committezen. No further research is required on the preferred format of commit information. Get immediate feedback on the format of the submit message on the command line and prompt for required fields.

Specification submission information
  1. The benefits of specification submission are as follows:
  • A clear history allows developers to track code efficiently
  • Automatically generate changelog. MD
  • Semantic version changes are automatically determined based on the type of commit
  • Triggers automated build and deployment processes based on the commit type
<type>[optional scope]: <subject> // Empty line [optional body] // Empty line [optional footer(s)]Copy the code

Type The category to be submitted. It must be one of the following:

Style: make changes that do not affect the meaning of the code, such as whitespaces, formatting, missing semicolons, etc. Add tests or update existing tests chore: updates to builds or ancillary tools or dependent librariesCopy the code

Without further ado, let’s get down to business.

Installing

First, install the Committeen CLI tool

npm install commitizen -D
Copy the code

Next, run the following command to initialize the project:

commitizen init cz-conventional-changelog --save-dev --save-exact
Copy the code

Or use the Yarn:

commitizen init cz-conventional-changelog --yarn --dev --exact
Copy the code

Then, add config.commitizen to the root directory of package.json, as shown below:

"config": {
    "commitizen": {
      "path": "cz-conventional-changelog"
    }
 }
Copy the code

Add the following script to package.json:

"scripts": {
  "commit" : "git-cz"
}
Copy the code
Now you can NPM run commit, as follows:
  • Git add. file
  • NPM run commit. At this point, Commitizen will ask us on the CLI:

  • After selecting the submission type, fill in the details according to the actual situation:

This completes the semi-automated specification submission.

Standard -version Automatic version control

Installing
npm i -D standard-version
Copy the code
inpackage.jsonAdd:
"scripts": {
  "commit" : "git-cz",
  "release": "standard-version" 
}
Copy the code
Now you can use NPM run Release by doing the following:
  • Git add. file
  • npm run commit
  • npm run release

The preceding operations complete automatic version control of Changelog. md.

Custom changelog. md is generated

By default, standard-version records only the submissions of feat and fix types in Changelog. md, which can be customized. The operations are as follows:

  • Create files in the root directory:.versionrc,.versionrc.json or.versionrc.js
  • For example, to create. Versionrc, configure the following information:
{
    "types": [
        {"type": "feat", "section": "Features"},
        {"type": "fix", "section": "Bug Fixes"},
        {"type": "chore", "hidden": true},
        {"type": "docs", "hidden": true},
        {"type": "style", "hidden": true},
        {"type": "refactor", "hidden": true},
        {"type": "perf", "hidden": true},
        {"type": "test", "hidden": true}
    ]
}
Copy the code
  • Type Indicates a string that matches Conventional Commits.
  • Section matches the part of the commit type that will be displayed in Changelog.md.
  • Hidden set to true to hide the matching commit type in Changelog.md.

Changelog. md- Refer to configuration information

Reference documents:

Standardize GIT code commit information & automate version management

The contract form submits the specification

Commitizen don’t commit git information!

commitizen – github

commitizen – npm

standard-version – npm

standard-version – github