git hook husky+commitlint檢查git提交描述是否符合規范


注意:npm 安裝 npm版本需要在7.0以上

第一步:安裝依賴:

yarn add @commitlint/config-conventional@12.1.4 @commitlint/cli@12.1.4 -S

第二步:創建commitlint.config.js,可以命令行執行以下代碼,或者手動創建文件,並添加標紅代碼

echo "module.exports={extends: ['@commitlint/config-conventional']}" > commitlint.config.js

第三步:打開commitlint.config.js, 增加配置項 

注意:確保當前文件格式為 “UTF-8”的編碼格式
module.exports = { // 繼承的規則 extends: ['@commitlint/config-conventional'], // 定義自己的規則 roles: { // type 的類型定義:表示 git 提交的 type 必須在以下范圍之內 'type-enum': [ // 當前驗證的錯誤級別, 2: 錯誤級別的錯誤 2, // 在什么情況襲進行驗證 'always', // 表示一直驗證 // 泛型內容 同cz-config.js中的types一樣 [ 'feat', // 新功能 feature 'fix', // 修復 bug 'docs', // 文檔注釋 'style', // 代碼格式(不影響代碼運行的變動) 'refactor', // 重構(既不增加新功能,也不是修復bug) 'perf', // 性能優化 'test', // 增加測試 'chore', // 構建過程或輔助工具的變動 'revert', // 回退 'build' // 打包 ] ], // subject 大小寫不做校驗 'subject-case': [0] } }

 

第四步:安裝 husky

yarn add husky@7.0.1 -S

第五步:手動啟動husky,生成.husky文件夾

npx husky install

第六步:在package.json中生成 prepare 指令, 命令行執行以下代碼 或者 手動添加指令

npm set-script prepare "husky install"

 

第七步:執行prepare指令

yarn prepare / npm run prepare

第八步: 添加 commitlint 的 hook 到 husky 中,並指令在commit-msg 的hooks下執行 npx --no-install commitlint --edit "$1" 指令

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

 

 

 第九步:測試

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM