【硅谷甄选】commitlint

配置 commitlint

对于我们的 commit 信息,也是有统一规范的,不能随便写,要让每个人都按照统一的标准来执行,我们可以利用commitlint来实现。

安装包

复制代码
pnpm add @commitlint/config-conventional @commitlint/cli -D

添加配置文件,新建 commitlint.config.cjs (注意是cjs),然后添加下面的代码:

复制代码
module.exports = {
  extends: ['@commitlint/config-conventional'],
  // 校验规则
  rules: {
    'type-enum': [
      2,
      'always',
      [
        'feat',
        'fix',
        'docs',
        'style',
        'refactor',
        'perf',
        'test',
        'chore',
        'revert',
        'build',
      ],
    ],
    'type-case': [0],
    'type-empty': [0],
    'scope-empty': [0],
    'scope-case': [0],
    'subject-full-stop': [0, 'never'],
    'subject-case': [0, 'never'],
    'header-max-length': [0, 'always', 72],
  },
}

package.json中配置scripts命令

复制代码
# 在scrips中添加下面的代码
{
"scripts": {
    "commitlint": "commitlint --config commitlint.config.cjs -e -V"
  },
}

配置结束,现在当我们填写commit信息的时候,前面就需要带着下面的subject

复制代码
'feat',//新特性、新功能
'fix',//修改bug
'docs',//文档修改
'style',//代码格式修改, 注意不是 css 修改
'refactor',//代码重构
'perf',//优化相关,比如提升性能、体验
'test',//测试用例修改
'chore',//其他修改, 比如改变构建流程、或者增加依赖库、工具等
'revert',//回滚到上一个版本
'build',//编译相关的修改,例如发布版本、对项目构建或者依赖的改动

配置husky

复制代码
npx husky add .husky/commit-msg 

在生成的commit-msg文件中添加下面的命令

复制代码
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
pnpm commitlint

当我们 commit 提交信息时,就不能再随意写了,必须是 git commit -m 'fix: xxx' 符合类型的才可以,需要注意的是类型的后面需要用英文的 :,并且冒号后面是需要空一格的,这个是不能省略的。

相关推荐
L-影13 小时前
FastAPI CORS 跨域:Web 世界的“小区门禁”与“访客通行证”
前端·javascript·fastapi
stereohomology13 小时前
已进入初赛提交Demo的两个项目:(2)单独html钢琴
前端·html·why不coding
源图客14 小时前
Claude Code基础使用
服务器·前端·数据库
asdzx6714 小时前
在 React 中轻松实现 PDF 转 Word:纯前端 WASM 方案实战
前端·react.js·pdf
爱勇宝1 天前
第 1 章:别把“需求文档”当成真正的需求
前端·后端·程序员
梨子同志1 天前
常用命令
前端
梨子同志1 天前
React 状态管理
前端
Dxy12393102161 天前
MySQL索引完整教程:创建、查看、修改、删除与日常管理
前端·javascript·mysql
剪刀石头布啊1 天前
js能被遍历的集合有哪些特征,为何能被遍历
前端
剪刀石头布啊1 天前
js解构
前端