用 Husky + Commitlint 自动化检查提交格式的踩坑经验分享
2023-11-01 21:07:08
最近新起了一个后台项目,还要迁移旧的项目代码,练手之余各种踩坑,临近上线才想起来添加 commit 提交代码前格式化检查代码并规范提交信息。 在团队多人开发中,规范的 commit message 可以方便代码审查和追踪问题,也便于未来回溯代码演进历史。
使用 Husky 和 Commitlint 进行提交格式检查非常方便,它们可以帮助团队成员保持一致的提交规范,同时也能减少代码审查的工作量。
但我们在使用 Husky 和 Commitlint 时也遇到了不少问题,比如:
* 配置不当导致提交失败
* 无法自动格式化代码
* 无法自定义提交信息模板
* 无法与其他工具集成
通过不断地尝试和调整,我们最终解决了这些问题,并实现了自动化的提交格式检查和规范提交信息。
**在本文中,我将分享在使用 Husky 和 Commitlint 时遇到的踩坑经验,并提供解决方案。**
一、配置不当导致提交失败
在配置 Husky 和 Commitlint 时,我们需要在项目根目录下创建两个文件:.huskyrc 和 .commitlintrc。这两个文件分别用于配置 Husky 和 Commitlint。
.huskyrc
[options]
提交前触发commitlint
commit-msg = 'commitlint -E HUSKY_GIT_PARAMS'
.commitlintrc
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"body-leading-blank": [1, "always"],
"body-max-line-length": [2, "always", 100],
"footer-leading-blank": [1, "always"],
"footer-max-line-length": [2, "always", 100],
"header-max-length": [2, "always", 100],
"subject-case": [2, "always", "lower-case"],
"subject-empty": [2, "never"],
"subject-full-stop": [2, "never"],
"type-case": [2, "always", "lower-case"],
"type-empty": [2, "never"],
"type-enum": [
2,
"always",
["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test"]
]
}
}
如果配置不当,则可能会导致提交失败。常见的问题包括:
* .huskyrc 或 .commitlintrc 文件的路径不正确
* .huskyrc 或 .commitlintrc 文件的语法不正确
* Husky 或 Commitlint 版本过低
**解决方案:**
* 确保 .huskyrc 和 .commitlintrc 文件的路径正确
* 确保 .huskyrc 和 .commitlintrc 文件的语法正确
* 更新 Husky 和 Commitlint 到最新版本
二、无法自动格式化代码
Husky 和 Commitlint 都支持自动格式化代码。但是,我们需要在项目中安装相应的代码格式化工具。常见的代码格式化工具包括:
* Prettier
* ESLint
* Stylelint
安装好代码格式化工具后,我们需要在 .huskyrc 文件中配置相应的命令。比如,如果我们要使用 Prettier,则可以在 .huskyrc 文件中添加以下内容:
"pre-commit": "prettier --write"
**解决方案:**
* 安装相应的代码格式化工具
* 在 .huskyrc 文件中配置相应的命令
三、无法自定义提交信息模板
Commitlint 允许我们自定义提交信息模板。但是,我们需要在 .commitlintrc 文件中配置提交信息模板。
.commitlintrc
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"body-leading-blank": [1, "always"],
"body-max-line-length": [2, "always", 100],
"footer-leading-blank": [1, "always"],
"footer-max-line-length": [2, "always", 100],
"header-max-length": [2, "always", 100],
"subject-case": [2, "always", "lower-case"],
"subject-empty": [2, "never"],
"subject-full-stop": [2, "never"],
"type-case": [2, "always", "lower-case"],
"type-empty": [2, "never"],
"type-enum": [
2,
"always",
["build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test"]
]
},
"prompt": {
"messages": {
"type": "Select the type of change that you're committing:",
"scope": "Denote the scope of this change (optional):",
"subject": "Write a short, imperative tense description of the change:",
"body": "Provide a longer description of the change (optional). Use line breaks to break into separate paragraphs:",
"footer": "List any breaking changes or references to other tickets (optional):"
},
"types": [
{ "value": "build", "name": "Changes that affect the build system or external dependencies (example: pip requirements)" },
{ "value": "chore", "name": "Changes to the build process or auxiliary tools and libraries such as documentation generation" },
{ "value": "ci", "name": "Changes to our CI configuration files and scripts (example: Docker, Jenkins, Travis)" },
{ "value": "docs", "name": "Documentation only changes" },
{ "value": "feat", "name": "A new feature" },
{ "value": "fix", "name": "A bug fix" },
{ "value": "perf", "name": "A code change that improves performance" },
{ "value": "refactor", "name": "A code change that neither fixes a bug nor adds a feature" },
{ "value": "revert", "name": "Reverts a previous commit" },
{ "value": "style", "name": "Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)" },
{ "value": "test", "name": "Adding missing tests or correcting existing tests" }
]
}
}
**解决方案:**
* 在 .commitlintrc 文件中配置提交信息模板
四、无法与其他工具集成
Husky 和 Commitlint 可以与其他工具集成,比如:
* GitLab CI/CD
* Jenkins
* Travis CI
集成之后,我们可以通过这些工具来触发 Husky 和 Commitlint。
**解决方案:**
* 根据需要选择合适的集成工具
* 配置集成工具以触发 Husky 和 Commitlint
五、总结
在本文中,我分享了在使用 Husky 和 Commitlint 时遇到的踩坑经验,并提供了解决方案。
希望这些经验能够帮助大家避免在使用 Husky 和 Commitlint 时遇到的问题。
总之,Husky 和 Commitlint 是非常有用的工具,它们可以帮助团队成员保持一致的提交规范,同时也能减少代码审查的工作量。