代碼提交信息規范插件commitlint/cli的安裝和使用
代碼提交信息規范插件commitlint/cli的安裝和使用
npm install --save-dev @commitlint/cli@^17.6.0 @commitlint/config-conventional@^17.6.0
新增commitlint.config.cjs
// commitlint.config.js
/**
* CommitLint 配置文件
* 該配置遵循 Conventional Commits 規范,適用于企業級提交校驗標準
*/
module.exports = {
// 使用社區通用規范
extends: ['@commitlint/config-conventional'],
// 自定義規則
rules: {
// 1. type 類型必須在以下列表中,保證語義清晰
'type-enum': [
2,
'always',
[
'feat', // 新功能 feature
'fix', // 修復 bug
'docs', // 文檔變更
'style', // 代碼格式(不影響功能,如空格、縮進)
'refactor', // 代碼重構(不包括修復和功能)
'perf', // 性能優化
'test', // 添加或修改測試
'build', // 構建工具變更(webpack、vite、npm scripts 等)
'ci', // CI 配置變更(GitHub Actions、GitLab CI 等)
'chore', // 其他日常事務(構建過程輔助腳本、依賴升級等)
'revert' // 回滾提交
]
],
// 2. type 必須小寫(例如 "Feat" 會報錯)
'type-case': [2, 'always', 'lower-case'],
// 3. scope(作用域)必須小寫,保持統一(如 "auth", "api")
'scope-case': [2, 'always', 'lower-case'],
// 4. subject(簡短描述)不能為空
'subject-empty': [2, 'never'],
// 5. subject 不允許以句號結尾(如 "fix: update foo." 會報錯)
'subject-full-stop': [2, 'never', ['.']],
// 6. subject 必須是句首大寫(Sentence case)或全部小寫(可按需修改)
'subject-case': [2, 'always', 'sentence-case'],
// 7. header(type + scope + subject)總長度不超過 72 字符
'header-max-length': [2, 'always', 72],
// 8. body 每行不能超過 72 字符,便于終端閱讀和 changelog 顯示
'body-max-line-length': [2, 'always', 72],
// 9. footer 每行不能超過 72 字符(如 BREAKING CHANGE、issue refs)
'footer-max-line-length': [2, 'always', 72],
// 10. footer 前必須有一個空行(與 body 分隔)
'footer-leading-blank': [2, 'always'],
}
};
echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg
測試
git commit -m "Feat(auth): add OAuth2 login support"
示例
git commit -m "feat(auth): add OAuth2 login support"
浙公網安備 33010602011771號