commit 提交规范校验--精简版(ts)

Husky

什么是 Husky

Husky就是让原生 git 钩子变得简单好操作的依赖包而已,你可以使用它来检查提交消息运行测试lint代码等…当你提交或推送时。Husky支持 所有 Git 钩子

使用 Husky

npm install husky --save-dev
# 或
pnpm add husky --save-dev            
  • package.json 文件的scripts配置项 添加一行代码
"scripts": {
    "prepare": "husky install"
}
  • 在命令行运行husky
npm run prepare
# 或
pnpm run prepare

commitlint

什么是 commitlint

就是对你 git commit提交的消息做一个校验的插件,主要还是配合Git Hooks的钩commit-msg一起使用的。

使用 commitlint

  • 安装相关依赖
# npm
npm install @commitlint/cli @commitlint/config-conventional @commitlint/types -D                      

# yarn
yarn add @commitlint/cli @commitlint/config-conventional @commitlint/types -D   

# pnpm        
pnpm add @commitlint/cli @commitlint/config-conventional @commitlint/types -D
  • 根目录下新建commitlint.config.ts 用来配置commitlint
import type { UserConfig } from "@commitlint/types";        

const CommitlintConfig: UserConfig = {
  extends: ["@commitlint/config-conventional"],
  // 校验规则
  rules: {
    "type-enum": [
      2,
      "always",
      [
        "init" /* 初始化 */,
        "feat" /* 添加新特性 */,
        "fix" /* 修改Bug */,
        "perf" /* 优化相关,比如提升性能,体验等  */,
        "style" /* 格式(非正常业务逻辑变动) */,
        "docs" /* 文档修改 */,
        "merge" /* 合并分支 */,
        "update" /* 更新 */,
        "refactor" /* 重构(即不是新增功能,也不是修改bug的代码变动) */,
        "test" /* 增加测试 */,
        "chore" /* 构建过程或辅助工具的变动 */,
        "revert" /* 代码版本回滚 */,
        "workflow" /* 添加工作流 */,
      ],
    ],
  },
};

export default CommitlintConfig;
  • package.json 中配置命令
"scripts": {
    ...
    "commitlint": "commitlint --config commitlint.config.ts -e -V",                                 
 }

启用commit

执行如下命令

npx husky add .husky/commit-msg "npm run commitlint"

会在根目录的.husky文件夹下多了一个 commit-msg 文件,其内容如下:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run commitlint

这样,commit-msg 这个钩子就开始生效了,再次提交代码就会开始检测你的提交消息了

完结 撒花

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值