Commitizen/CZ-CLI is a tool to standardize the submission of information. When we are in the process of collaborative development, it is necessary to develop a good commit specification, which can be used to review the code, produce CHANGELOG instructions, etc.

Install Commitizen locally

npm install --save-dev commitizen
Copy the code

To customize the submission instructions for the project, you can use the CZ-Customizable adapter:

npm install cz-customizable --save-dev
Copy the code

In package.json plus configuration using node_modules/cz-customizable adapter

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

Cz-customizable can read the.z-config. js configuration of the project by default, so we need to create a new file. Cz-config. js.

The processing is as follows:

'use strict';

module.exports = {

  types: [{value: ':construction: WIP'.name: '💪 WIP: Work in Progress '
    },
    {
      value: ':sparkles: feat'.name: '✨ feat: A new feature'
    },
    {
      value: ':bug: fix'.name: '🐛 fix: A bug fix'
    },
    {
      value: ':hammer: refactor'.name: '🔨 refactor: A code change that neither fixes A bug nor adds A feature'
    },
    {
      value: ':pencil: docs'.name: '📝 docs: Documentation only changes'
    },
    {
      value: ':white_check_mark: test'.name: '✅ test: Add missing tests or existing tests'
    },
    {
      value: ':thought_balloon: chore'.name: '🗯 chore: Changes that don\'t modify SRC or test files. Such as updating build tasks, package Manager '
    },
    {
      value: ':lipstick: ui'.name: '💄 Updating the UI and style files.'}, {value: ':art: style'.name:
        '🎨 Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)'}, {value: 'revert'.name: '⏪ Revert: Revert to a commit'
    },
    {
      value: ':package: dep_up'.name: '📦 Updating compiled files or packages.'}, {value: ':green_heart: fixci'.name: '💚 Fixing a CI Build."}, {value: ':truck: mv'.name: '🚚 Moving or renaming files.'}, {value: ':fire: prune'.name: '🔥 Removing code or files.'}, {value: ':bookmark: release'.name: '🔖 Releasing/Version tags.'}, {value: ':rocket: first release'.name: '🚀 first releast! ',}],scopes: [].allowCustomScopes: true.allowBreakingChanges: ["feat"."fix"]};Copy the code

All right, let’s see what happens

> git add. > NPX git czCopy the code

Git add. > git commit -m ‘xx’

Submit a check

This is a two-step process that requires installing CommitLint and Husky

Step 1 Install CommitLint

To prevent non-conforming commit information from being committed in the project, we also need to add another tool, CommitLint

npm install --save-dev @commitlint/{cli,config-conventional}
Copy the code

Verify the custom commit instructions using commitlint-config-cz.

npm install commitlint-config-cz --save-dev
Copy the code

Create.commitlintrc.js in the root directory, and configure the declaration to inherit CZ

'use strict';

module.exports = {
	extends: ["cz"],
	rules: {
		'type-empty': [2, 'never'], 
		'subject-empty': [2, 'never'] 
	}
};

Copy the code

Step 2 Install Husky

Husky is a tool that contains hooks that need to be checked during Git commit time. Husky is a tool that contains hooks that need to be checked during Git commit time.

npm install --save-dev husky
Copy the code
// package.json
{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}}}Copy the code

Try running it:

Why is there an error here? After checking issues#39, we found that commitlint does not support emoji, so we just need to remove emojis from the.z-config.js file.

What if we want to support emoji checks? Look at step three:

Step 3: Support emoji verification (optional)

If the commit message has no expression, skip this step

Commitlint-config-gitmoji: Commitlint-config-gitmoji

npm i -D commitlint-config-gitmoji @commitlint/core
Copy the code

Add commitlint config for Gitmoji

'use strict';

module.exports = {
+	extends: ["./node_modules/commitlint-config-gitmoji","cz"],
	rules: {
+ 		'type-empty': [2, 'never',[
+ 			':art:',
+			':newspaper:',
+			':pencil:',
+			':memo:',
+			':zap:',
+			':fire:',
+			':books:',
+			':bug:',
+			':ambulance:',
+			':penguin:',
+			':apple:',
+			':checkered_flag:',
+			':robot:',
+			':green_ale:',
+			':tractor:',
+			':recycle:',
+			':white_check_mark:',
+			':microscope:',
+			':green_heart:',
+			':lock:',
+			':arrow_up:',
+			':arrow_down:',
+			':fast_forward:',
+			':rewind:',
+			':rotating_light:',
+			':lipstick:',
+			':wheelchair:',
+			':globe_with_meridians:',
+			':construction:',
+			':gem:',
+			':bookmark:',
+			':tada:',
+			':loud_sound:',
+			':mute:',
+			':sparkles:',
+			':speech_balloon:',
+			':bulb:',
+			':construction_worker:',
+			':chart_with_upwards_trend:',
+			':ribbon:',
+			':rocket:',
+			':heavy_minus_sign:',
+			':heavy_plus_sign:',
+			':wrench:',
+			':hankey:',
+			':leaves:',
+			':bank:',
+			':whale:',
+			':twisted_rightwards_arrows:',
+			':pushpin:',
+			':busts_in_silhouette:',
+			':children_crossing:',
+			':iphone:',
+			':clown_face:',
+			':ok_hand:',
+			':boom:',
+			':bento:',
+			':pencil2:',
+			':package:',
+			':alien:',
+			':truck:',
+			':age_facing_up:',
+			':busts_in_silhouette:',
+			':card_file_box:',
+			':loud-sound:',
+			':mute:',
+			':egg:',
+			':see-no-evil:',
+			':camera-flash:',
+			':alembic:',
+			':mag:',
+			':wheel-of-dharma:',
+			':label:'
+		]], 
		'subject-empty': [2, 'never'] 
	}
};
Copy the code

A Changelog log is generated

When we want to release a version, the usual way is to update the version number in package.json, update the CHANGELOG, describe what action is being updated, and then git add -> git commit, tag it, With standard-version, you can run it automatically.

The installation

npm i --save-dev standard-version
Copy the code

perform

npx standard-version
Copy the code

Write the script

We write the above NPX command into package.json script for easy invocation

  // package.json
  "scripts": {
    "commit": "git-cz",
    "release": "standard-version",
    "push:publish": "npm run build && git push --follow-tags origin master && npm publish"
  },
Copy the code

Above, finish ~

GitHub – Cz – CLI tool GitHub – Cz – CustomIZABLE adapter CommitLint Check Husky hook GitHub – Commitlint-config-gitmoji emojis support standard-version commitlint-config-cz/issue #39 COMMITlint /issue #1221