Z-git uses a trick to make git commit no longer a hassle

Today, WE have finally completed the documentation of CZ-git.qbenben.com/. Welcome to check and use it

Here are some tips for using CZ-Git

scopes

In the Commit Message, the usual scopes represent the scopes covered this time, and there are usually two scopes:

  • Project code level as you useyarnCooperate withlernaOr usepnpmManagement of monorepo
  • Project business level such as (Account) Account system related, (comment) comment system related…

Project code level

If you are using Monorepo packages as scopes, you can define them this way

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

Of course, if you define scope-enum using commitlint rules, it will be introduced 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 ]]
  }
};
Copy the code

Project Business Level

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

Of course, if you want to add a description to the module scope custom display on the command line, you can use the name and value properties to define it

// .commitlintrc.js 
const fs = require('fs')
const path = require('path')
const packages = fs.readdirSync(path.resolve(__dirname, 'packages'))
module.exports = {
  prompt: {
    scopes: [{value: "home".name: "Home: Home related" },
      { value: "account".name: "account:   账户相关" },
      { value: "comment".name: "Comment: Relevant"}}},]Copy the code

issuePrefixs

If you are using Gitee for project management, you can use the commit message to change the issue status: Gitee Commit Associated Issue You can set the task status command, that is, set the alias of the Issue status change. You can select the alias and enter the Issue number to associate and manage issues

// .commitlintrc.js 
module.exports = {
  prompt: {
    issuePrefixs: [
      // @see: https://gitee.com/help/articles/4141#article-header2
      { value: "wip".name: "Wip: Change the task status to In Progress" },
      { value: "finish".name: "Finish: Change the task status to pending"}}}]Copy the code

defaultIssues

Github’s issue number is usually a number, but Gitee is a bit anti-human, so I need to copy and paste in the branch name or search for the issue number in the webpage every commit. If our branch name has a set of specifications, such as fix/issue_I72636_qb, I can do this:

  • usingNode 的 execSyncThe branch name is obtained by command at run time
  • The obtained string is then processed
  • And then we usedefaultIssuesProperties of the incoming
  • To use it, we just press it< Enter >Key to outputIssue NumberSo we can intercept it easilyIssue NumberReduce repetitive tasks.
// .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}`}};Copy the code

The above is my share, I hope you open up the pattern, enjoy the process of exploring configuration to make the CLI more suitable for you or your team, welcome to share ~

My development process: juejin.cn/post/706975… The original document links: cz-git.qbenben.com/zh/guide/re… Github address (welcome star) : github.com/Zhengqbbb/c…