Standardized commit message submission information, commit verification, Standard Version automatic version replacement and changelog output

Keywords: node.js Front-end git

Auxiliary submission tool

Because you need to use the commit tool to assist in submitting information

npm install commitizen -g

Then you need to install the node environment first

use

Under project directory

# npm init is required first
commitizen init cz-conventional-changelog --save --save-exact

Submit using git cz
The specification will pop up for a series of input

Specific specifications and standards

The Commit message consists of three parts: Header, Body and Footer

<type>(<scope>): <subject>
<Blank line>
<body>
<Blank line>
<footer>

Note: Body and Footer are optional

type

typemeaning
featNew features
fixFix bug
docsModify document
refactorCode refactoring without adding any functions and fixing any bug s
buildChange the construction process, add dependency libraries, tools, etc. (e.g. webpack modification)
styleOnly spaces, indents, etc. are modified without changing the code logic
perfImproved performance and embodied modifications
choreModification of non src and test
testModification of test cases
ciAutomatic process configuration modification
revertRollback to previous version

scope

It is used to explain the scope of the impact of commit, such as data layer, control layer, view layer, package name, file name, etc.

subject

It is a short description of the purpose of commit, no more than 50 characters, with no period at the end.

Body

The Body part is a detailed description of this commit. It should explain the motivation of code changes and the comparison with previous behaviors.

Footer

  1. Are there any significant changes (breakthrough changes)
    If the framework changes or the previous version is incompatible, the Footer section starts with BREAKING CHANGE, followed by a description of the change, the reason for the change and the migration method.
  2. Does this change affect any unresolved issues (close issue)
    You can fill in fix #1 equal to close question 1, or use Closes #1 to close multiple issues. Use fix #1,#2,#3 comma to separate and close three issues.

Force validation of submitted information

yorkie is used to execute git hooks

npm install --D yorkie
// package.json
"gitHooks": {
    "commit-msg": "node git-hooks/verify-commit-msg.js"
}

verify-commit-msg.js

const chalk = require('chalk')
const msgPath = process.env.GIT_PARAMS
const msg = require('fs').readFileSync(msgPath, 'utf-8').trim()

const commitRE = /^(revert: )?(feat|fix|polish|docs|style|refactor|perf|test|workflow|ci|chore|types|build)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
  console.error(
    `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(`invalid commit message format.`)}\n\n` +
    chalk.red(`  Proper commit message format is required for automated changelog generation. Examples:\n\n`) +
    `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
    `    ${chalk.green(`fix(v-model): handle events on blur (close #28)`)}\n\n` +
    chalk.red(`  You can also use ${chalk.cyan(`npm run commit`)} to interactively generate a commit message.\n`)
  )
  process.exit(1)
}

Custom gitHooks

standard-version

Conventional changelog cli generate change log

CZ conventional changelog can automatically generate a change log based on the submitted information

$ npm install -g conventional-changelog-cli

implement

# Stack on the basis of the previous generation
$ conventional-changelog -p angular -i CHANGELOG.md -s
# Generate all records, including previous
$ conventional-changelog -p angular -i CHANGELOG.md -s -r 0

This method doesn't work well when I use it. There is no corresponding relationship between version and change

Using standard version

reference resources

npm install -D standard-version

You can add scripts in package.json

"scripts": {
  "release": "standard-version"
}

Configuration for exporting CHANGELOG.md information
Create a. versionrc file as follows:

{
  "types": [
    {"type": "chore", "section":"Others", "hidden": false},
    {"type": "revert", "section":"Reverts", "hidden": false},
    {"type": "feat", "section": "Features", "hidden": false},
    {"type": "fix", "section": "Bug Fixes", "hidden": false},
    {"type": "improvement", "section": "Feature Improvements", "hidden": false},
    {"type": "docs", "section":"Docs", "hidden": false},
    {"type": "style", "section":"Styling", "hidden": false},
    {"type": "refactor", "section":"Code Refactoring", "hidden": false},
    {"type": "perf", "section":"Performance Improvements", "hidden": false},
    {"type": "test", "section":"Tests", "hidden": false},
    {"type": "build", "section":"Build System", "hidden": false},
    {"type": "ci", "section":"CI", "hidden":false}
  ]
}

Posted by anolan13 on Sun, 19 Sep 2021 18:24:28 -0700