項目中使用commitizen+commitlint規(guī)范提交

安裝相關(guān)依賴

npm install commitizen cz-customizable @commitlint/cli @commitlint/config-conventional -D

版本號如下:

{
    "@commitlint/cli": "^17.5.0",
    "@commitlint/config-conventional": "^17.4.4",
    "commitizen": "^4.3.0",
    "cz-conventional-changelog": "^3.3.0",
    "cz-customizable": "^7.0.0",
}

cz-conventional-changelog和cz-customizable安裝其中一個即可,當前是安裝cz-customizable來自定義提交信息
在package.json中配置commitizen的目錄

{
   "config": {
    "commitizen": {
      "path": "./node_modules/cz-customizable"
    }
  },

}

新建.cz-config.js文件

module.exports = {
  types: [//描述修改的性質(zhì)是什么,是bugfix還是feat,在這里進行定義。
    { value: 'feat', name: 'feat??:新功能' },
    { value: 'fix', name: 'fix??: 修復Bug' },
    { value: 'docs', name: 'docs??: 文檔更新' },
    {
      value: 'style',
      name: 'style??: 樣式更新',
    },
    {
      value: 'refactor',
      name: 'refactor??:重構(gòu)了',
    },
    { value: 'test', name: 'test??:測試代碼' },
  ],
  //定義之后,我們就可以通過上下鍵去選擇 scope
  // scopes: [{ name: 'accounts' }, { name: 'admin' }, { name: 'exampleScope' }, { name: 'changeMe' }],

  usePreparedCommit: false, // to re-use commit from ./.git/COMMIT_EDITMSG
  allowTicketNumber: false,
  isTicketNumberRequired: false,
  ticketNumberPrefix: 'TICKET-',
  ticketNumberRegExp: '\\d{1,5}',

  // it needs to match the value for field type. Eg.: 'fix'
  /*針對每一個type去定義scope

  scopeOverrides: {
    fix: [
      {name: 'merge'},
      {name: 'style'},
      {name: 'e2eTest'},
      {name: 'unitTest'}
    ]
  },
  */
  // override the messages, defaults are as follows
  messages: {
    type: "選擇你要提交的類型",
    // scope: '\nDenote the SCOPE of this change (optional):',
    // used if allowCustomScopes is true
    // customScope: 'Denote the SCOPE of this change:',
     subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
    // body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
    // breaking: 'List any BREAKING CHANGES (optional):\n',
    // footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
    confirmCommit: 'Are you sure you want to proceed with the commit above?',
  },
  // 設(shè)置為 true,在 scope 選擇的時候,會有 empty 和 custom 可以選擇,顧名思義,選擇 empty 表示 scope 缺省,如果選擇 custom,則可以自己輸入信息
  allowCustomScopes: true,
  //如上設(shè)置為 ['feat', 'fix'],只有我們type選擇了 feat 或者是 fix,才會詢問我們 breaking message.
  allowBreakingChanges: ['feat', 'fix'],
  // 指定跳過哪些步驟,例如跳過我們剛剛說的詳細描述,設(shè)置其為 scope: ['body'],假設(shè)我們的項目也不會涉及到關(guān)聯(lián) issue,我們可以設(shè)置其為 scope: ['body', 'footer']
  skipQuestions: ['scope','customScope','breaking','footer','body'],

  // 描述的長度限制
  subjectLimit: 100,
  // breaklineChar: '|', // It is supported for fields body and footer.
  // footerPrefix : 'ISSUES CLOSED:'
  // askForBreakingChangeFirst : true, // default is false
};

新建commitlint.config.js

module.exports = {
  extends: ["@commitlint/config-conventional"],
  rules: {
    "type-enum": [
      2,
      "always",
      [
        "WIP", // 開發(fā)中
        "feat", // 新特性
        "improvement", // 加強現(xiàn)有特性
        "fix", // 修補bug
        "refactor", // 重構(gòu)
        "docs", // 文檔
        "test", // 單元測試
        "config", // 配置文件
        "style", // 格式需改
        "perf", // 性能提升
        "ci", // ci
        "revert", // 版本回退
        "chore", // 其他修改
      ],
    ],
    "type-empty": [2, "never"], // type不能為空
    "type-case": [0, "always", "lower-case"], // type不限制大小寫
    "subject-empty": [2, "never"], // subject(簡短得描述)不能為空
    "subject-max-length": [1, "always", 50], // subject最大長度,超出只會警告,不阻止提交
    "body-leading-blank": [1, "always"],
    "footer-leading-blank": [1, "always"],
    "header-max-length": [2, "always", 72],
  },
};

安裝husky,安裝了可忽略

npm install husky -D

新建commit-msg文件并寫入hook執(zhí)行命令

npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"

以上完成在git commit 發(fā)起提交前執(zhí)行commitlint檢查是否符合規(guī)范??墒褂胓it cz 命令代替git commit提交代碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容