现代前端工程化实践:Git、Husky、Commitlint与PNPM的协同作战

引言

Git HuskyCommitlint 是两个在 Git 工作流程中非常实用的工具,它们可以帮助团队维护代码质量和提交规范。Husky 是一个 Git 钩子管理器,允许你在仓库级别方便地配置钩子脚本;而 Commitlint 则是用来规范 Git 提交信息的工具,确保每次提交都遵循一定的格式标准。下面是一个关于如何使用这两个工具的简明教程,以及如何进行基本配置。

使用 Husky 和 Commitlit 的版本

"husky": "^9.0.11",
"@commitlint/cli": "^19.3.0",
"@commitlint/config-conventional": "^19.2.2",

安装 Husky 和 Commitlint

首先,你需要在项目中安装 HuskyCommitlint,以及 Commitlint 的一个预设规则库(如 @commitlint/config-conventional)来定义提交信息的格式规范。

npm install --save-dev husky @commitlint/cli @commitlint/config-conventional

配置 Husky

接下来,配置 Husky 以便在 git commit 命令执行前自动运行 Commitlint 检查。

init 命令简化了项目中的 husky 设置。它会在 .husky/ 中创建 pre-commit 脚本,并更新 package.json 中的 prepare 脚本。随后可根据你的工作流进行修改。

pnpm exec husky init

执行后的效果如下:

package.json 文件中 script 新增一行脚本:

配置 Commitlint

Commitlint 需要一个配置文件来定义提交信息的规则。通常这个文件名为 commitlint.config.js.commitlintrc.json,位于项目根目录。这里我们使用 JavaScript 配置文件作为示例:

// .commitlint.config.js

export default {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'type-enum': [
      2,
      'always',
      [
        'bug',      // 此项特别针对bug号,用于向测试反馈bug列表的bug修改情况
        'feat',     // 新功能(feature)
        'fix',      // 修补bug
        'docs',     // 文档(documentation)
        'style',    // 格式(不影响代码运行的变动)
        'refactor', // 重构(即不是新增功能,也不是修改bug的代码变动)
        'test',     // 增加测试
        'chore',    // 构建过程或辅助工具的变动
        'revert',   // feat(pencil): add ‘graphiteWidth’ option (撤销之前的commit)
        'merge'     // 合并分支, 例如: merge(前端页面): feature-xxxx修改线程地址
      ]
    ]
  }
}

在这个配置中,我们继承了 @commitlint/config-conventional 的默认规则,并可以自定义一些规则来满足特定项目需求。

添加 commit-msg 钩子,提交时自动检测提交信息

.husky 目录下新建文件且没有后缀,名字是: commit-msg

pnpm dlx commitlint --edit $1

# $1 表示传递的第一个参数

实践提交

现在,当你尝试执行 git commit 时,Husky 会触发 Commitlint 对你的提交信息进行检查。如果格式不正确,它会给出错误信息并要求你修改。

  • 正确的提交信息应该是这样的:
git commit -m "fix: 修复页面的样式问题"

其中,“fix”是提交类型的简短描述,冒号后紧跟空格和对本次提交的详细描述。

  • 错误的提交信息如下:
git commit -m "新增 husky commitlit"

不出意外的话,将会报如下错误,并且提交中断

> husky-vue3-template@0.0.0 format G:\wokespace\github\husky-vue3-template
> eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore

.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 1, reused 0, downloaded 0, added 0
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 114, reused 95, downloaded 0, added 0
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 124, reused 124, downloaded 0, added 0
.../packages/pnpm/store/v3/tmp/dlx-4288  | +125 +++++++++++++
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 125, reused 125, downloaded 0, added 29
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 125, reused 125, downloaded 0, added 30
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 125, reused 125, downloaded 0, added 115
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 125, reused 125, downloaded 0, added 116
.../packages/pnpm/store/v3/tmp/dlx-4288  | Progress: resolved 125, reused 125, downloaded 0, added 125, done
⧗   input: 新增 husky commitlit
✖   subject may not be empty [subject-empty]type may not be empty [type-empty]

✖   found 2 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint.......

总结

通过结合 HuskyCommitlint,你可以有效地提高代码仓库的管理效率,确保每个提交都遵循一致的格式和高质量标准。这不仅有利于团队成员之间的沟通,也有助于自动化工具更好地理解和处理提交历史。希望这篇教程能帮助你顺利地在项目中应用这两个强大的工具。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-08 08:10:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-08 08:10:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-08 08:10:06       82 阅读
  4. Python语言-面向对象

    2024-06-08 08:10:06       91 阅读

热门阅读

  1. PyWhatKit ,一个全自动发送消息多功能的Pyhton库

    2024-06-08 08:10:06       31 阅读
  2. npm安装依赖过慢

    2024-06-08 08:10:06       31 阅读
  3. ArcGIS for js 4.x FeatureLayer 加载、点选、高亮

    2024-06-08 08:10:06       24 阅读
  4. 微信小程序和支付宝小程序生成二维码

    2024-06-08 08:10:06       27 阅读
  5. mysql 如何分布式部署

    2024-06-08 08:10:06       29 阅读
  6. 公司面试题总结(一)

    2024-06-08 08:10:06       25 阅读
  7. 正则表达式

    2024-06-08 08:10:06       29 阅读
  8. Spring Boot 常用注解

    2024-06-08 08:10:06       27 阅读
  9. Ajax + Easy Excel 通过Blob实现导出excel

    2024-06-08 08:10:06       29 阅读
  10. ARCGIS 几种SHP融合、拼接等方法

    2024-06-08 08:10:06       31 阅读