提交规范

Posted chenhuahua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了提交规范相关的知识,希望对你有一定的参考价值。

提交规范笔记

利用 inquirer 选择配置好的提交类型,以及配合 commitlint 实现 commit 检查
npm i inquirer shelljs @commitlint/{cli,config-conventional} -D
添加 package.json 的 script

"commitlint": "commitlint -e",
"commit": "node commit/git-commit.js" 

创建 commit/git-commit.js 文件

const shell = require(\'shelljs\')
const inquirer = require(\'inquirer\')
const prompsConfig = {
  ciType: [
    {
      type: \'list\',
      name: \'type\',
      message: \'请选择本次提交的类型:\',
      choices: [
        {
          name: \'引入新特性\',
          value: \'feat\',
        },
        {
          name: \'改进代码的结构格式/样式\',
          value: \'style\',
        },
        {
          name: \'修复 bug\',
          value: \'fix\',
        },
        {
          name: \'提升性能\',
          value: \'perf\',
        },
        {
          name: \'删除代码或文件\',
          value: \'delete\',
        },
        {
          name: \'其他修改, 比如改变构建流程、或者增加依赖库、工具等\',
          value: \'chore\',
        },
        {
          name: \'重构\',
          value: \'refactor\',
        },
        {
          name: \'撰写文档\',
          value: \'docs\',
        },
        {
          name: \'增加测试\',
          value: \'test\',
        },
        {
          name: \'更新打包文件\',
          value: \'build\',
        },
        {
          name: \'初次提交\',
          value: \'init\',
        },
        {
          name: \'发布/版本标签\',
          value: \'release\',
        },
        {
          name: \'部署功能\',
          value: \'deploy\',
        },
        {
          name: \'代码回滚\',
          value: \'revert\',
        },
        {
          name: \'CI持续集成修改\',
          value: \'ci\',
        },
      ],
    },
  ],
  ciMsg: {
    type: \'input\',
    name: \'msg\',
    message: \'请输入提交文本:\',
    validate: function (value) {
      if (value) {
        return true
      }
      return \'文本必须输入!\'
    },
  },
}

async function gitCommit() {
  let { type } = await inquirer.prompt(prompsConfig.ciType)
  let { msg } = await inquirer.prompt(prompsConfig.ciMsg)

  shell.exec(`git commit -m "${type}: ${msg}"`, function () {
    console.log(`\\n提交脚本: git commit -m "${type}: ${msg}"`)
  })
}

gitCommit()

配置 commitlint 类型,创建 commitlint.config.js 文件

module.exports = {
  extends: [\'@commitlint/config-conventional\'],
  rules: {
    \'type-enum\': [2, \'always\', [
      \'build\', \'chore\', \'ci\', \'feat\', \'docs\', \'fix\', \'perf\', \'revert\', \'refactor\', \'style\', \'test\', \'init\', \'build\', \'release\', \'delete\'
     ]],
  }
};

以上是关于提交规范的主要内容,如果未能解决你的问题,请参考以下文章

Git提交规范

Git(三)代码提交规范

Git提交规范

Git Commit Log提交规范

Git代码提交规范

Git代码提交规范