项目添加commitlint提交验证

发布时间 2023-09-06 09:17:41作者: 童心虫鸣

安装

  • 如果根目录没有package.json ,先添加,如果请跳过
pnpm init
  • 安装依赖
pnpm add @kvuse/commitlint-config @commitlint/cli -D

使用

使用命令添加配置文件

cd node_modules/@kvuse/commitlint-config

pnpm run preinstall

执行命令成功提示

> @kvuse/commitlint-config@1.1.0 preinstall /Users/liukai/Documents/work/husky-demo/node_modules/.pnpm/@kvuse+commitlint-config@1.1.0/node_modules/@kvuse/commitlint-config
> node prepare

添加.commitlintrc.json成功
添加.husky成功

添加提交eslint验证

  • package.json添加lint

    如果有请忽略

      "scripts": {
        "lint": "eslint ."
      },
    
  • .husky 添加 npm run lint命令

    // .husky/commit-msg
    #!/usr/bin/env sh
    . "$(dirname -- "$0")/_/husky.sh"
    
    npx --no -- commitlint --edit "$1"
    npm run lint
    

测试

git add .
git commit -m 'Feat更'

如果出现以下报错就成功了

⧗   input: Feat更
✖   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

husky - commit-msg hook exited with code 1 (error)

规则

[
  'feat', // 新功能(feature)
  'fix', // 修补bug
  'docs', // 文档(documentation)
  'style', // 格式(不影响代码运行的变动)
  'refactor', // 重构
  'test', // 增加测试
  'revert', // 回滚
  'config', // 构建过程或辅助工具的变动
  'chore', // 其他改动
],
  • 开头必须有上述字段,例如:feat: 添加xx功能
  • 提交内容不能小于三个字符