paint-brush
Cz-git Recipes - Easy to Commitā€‚by@zhengqbbb
833 reads
833 reads

Cz-git Recipes - Easy to Commit

by ZhengqbbbApril 7th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

cz-git is a Node.js-based `git commit` command-line tool that assists in generating standardized and standardized commit messages. It replaces theĀ interactiveĀ plugin for the commitizen command line tool.

Company Mentioned

Mention Thumbnail
featured image - Cz-git Recipes - Easy to Commit
Zhengqbbb HackerNoon profile picture


github: https://github.com/Zhengqbbb

website: https://cz-git.qbenben.com/

What is cz-git?

What is commitizen: A Node.js-basedĀ git commitĀ command-line tool that assists in generating standardized and standardized commit messages.


What is an adapter: Replace theĀ interactiveĀ plugin for the commitizen command line tool.


Why do it

For the past year, I have been committing almost every day, and as a lazy software engineer, I realized that almost all commitizen adapters are not very comfortable for me to use.


For example, if I repaired the table in the monorepo ui library, I must subconsciously fix the table. Why do I need to keep selecting up and down? I just want fi to press Enter to output fix, ta to press Enter to output table.So much so that I made the cz-git adapter.


recipes

Let me introduce the use of cz-git


scopes

scopes, usually to define the scope of this commit, there are generally two types: according to theĀ project code distinctionĀ such as monorepo , the other isĀ project business distinction


scopes for project code

If you need to manage multiple packages for a better experience, for example, use:Ā pnpmĀ |Ā lerna.jsĀ to manage monorepo you can Use theĀ pathĀ andĀ fsĀ modules to dynamically define the scopes (scopes) display in the commit message.

// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: { 
    scopes: [...packages] 
  }
}


If you define aĀ scope-enumĀ using theĀ commitlintopen in new windowĀ rule, it will be imported automatically.

// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  rules: {
    "scope-enum": [2, "always", [ ...packages ]]
  }
};


demo-gif

scopes for business system

// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: ["app", "home", "account", "comment"] 
  }
}


Of course, if you want to addĀ description informationĀ to the module-wide customization to display on the command line, you can useĀ nameĀ andĀ valueĀ to define.

// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [
      { value: "app",     name: "app:       System business" },
      { value: "home",    name: "home:      Homepage" },
      { value: "account", name: "account:   Account related" },
      { value: "comment", name: "comment:   Comment related" },
    ]
  }
}


demo-gif

default

defaultIssues

  • Obtaining theĀ Issue NumberĀ automatically, it is a very troublesome thing to repeat the query to fill in theĀ issue number.
    • But if the team's branch command rules are standardized (e.g: fix/33)
    • Then we useĀ Node'sĀ execSyncĀ to get the branch name through the command
    • Then process the obtained string
    • Then we useĀ defaultIssues
    • When using, we only need to press theĀ <Enter>Ā key to output theĀ Issue Number, so that we can easily intercept theĀ Issue NumberĀ to reduce repetitive work.
// .commitlintrc.js 
const { execSync } = require('child_process');

// @tip: git branch name = feature/33   =>    auto get defaultIssues = #33
 const issue = execSync('git rev-parse --abbrev-ref HEAD')
  .toString()
  .trim()
  .split("/")[1]
// @tip: monorepo dynamic get name

/** @type {import('cz-git').UserConfig} */
module.exports = {
  prompt: {
    defaultIssues: () => !issue ? "" : `#${issue}`
  }
};


Expand your imagination, and the highly customizableĀ cz-gitĀ makes committing more convenient and more customary. Welcome to share.