安裝相關依賴
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: [//描述修改的性質是什么,是bugfix還是feat,在這里進行定義。
{ value: 'feat', name: 'feat??:新功能' },
{ value: 'fix', name: 'fix??: 修復Bug' },
{ value: 'docs', name: 'docs??: 文檔更新' },
{
value: 'style',
name: 'style??: 樣式更新',
},
{
value: 'refactor',
name: 'refactor??:重構了',
},
{ 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?',
},
// 設置為 true,在 scope 選擇的時候获枝,會有 empty 和 custom 可以選擇积蜻,顧名思義闯割,選擇 empty 表示 scope 缺省,如果選擇 custom竿拆,則可以自己輸入信息
allowCustomScopes: true,
//如上設置為 ['feat', 'fix']宙拉,只有我們type選擇了 feat 或者是 fix,才會詢問我們 breaking message.
allowBreakingChanges: ['feat', 'fix'],
// 指定跳過哪些步驟丙笋,例如跳過我們剛剛說的詳細描述谢澈,設置其為 scope: ['body'],假設我們的項目也不會涉及到關聯(lián) issue御板,我們可以設置其為 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", // 重構
"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ī)范敬鬓。可使用git cz 命令代替git commit提交代碼