Husky + Lint-staged + Commitlint + Commitizen来配置 Git 提交代码规范

本文介绍 如何通过 Husky + Lint-staged + Commitlint + Commitizen来配置 Git 提交代码规范。

核心内容是配置 Husky 的 pre-commitcommit-msg 两个钩子:

pre-commit:Husky + Lint-staged 整合实现 Git 提交前代码规范检测/格式化 (前提:ESlint + Prettier + Stylelint 代码统一规范);

commit-msg: Husky + Commitlint + Commitizen 整合实现生成规范化且高度自定义的 Git commit message。

官方推荐自动安装的方式,使用 husky-init 命令一次性完成依赖自动安装和配置

npx husky-init
npm install

自动生成的 .husky 目录和指令:

在这里插入图片描述

Lint-staged

lint-staged 是一个在 git add 到暂存区的文件运行 linters (ESLint/Prettier/StyleLint) 的工具,避免在 git commit 提交时在整个项目执行。

官方网站:https://github.com/okonet/lint-staged

Lint-staged 安装

npm install -D lint-staged

Lint-staged 配置

检测/格式化配置

注意如果是旧项目集成时,需要先进行npm run lint 进行格式化手动处理一些问题。

在这里插入图片描述

修改提交前钩子命令

根目录 .husky 目录下 pre-commit 文件中的 npm test 修改为 lint-staged --allow-empty

添加提交信息校验钩子

执行下面命令生成 commint-msg 钩子用于 git 提交信息校验,命令来自:官网

npx husky add .husky/commit-msg "npx --no -- commitlint --edit $1"

Commitlint

Commitlint 检查您的提交消息是否符合 Conventional commit format。– Commitlint 官网

Commitlint 安装

参考 官方安装文档

Commitlint 配置

根目录创建 commitlint.config.cjs 配置文件,示例配置: @commitlint/config-conventional

module.exports = {
  // 继承的规则
  extends: ["@commitlint/config-conventional"],
  // @see: https://commitlint.js.org/#/reference-rules
  rules: {
    "subject-case": [0], // subject大小写不做校验

    // 类型枚举,git提交type必须是以下类型
    "type-enum": [
      2,
      "always",
      [
        'build', // 构建流程、外部依赖变更(如升级 npm 包、修改 webpack 配置等)
        'chore', // 对构建过程或辅助工具和库的更改(不影响源文件、测试用例)
        'ci', // 修改 CI 配置、脚本
        'docs', // 文档变更
        'feat', // 新增功能
        'fix',  // 修复缺陷
        'perf', // 性能优化
        'refactor', // 代码重构(不包括 bug 修复、功能新增)
        'revert', // 回滚 commit
        'style', // 代码格式(不影响功能,例如空格、分号等格式修正)
        'test', // 添加疏漏测试或已有测试改动
        'text', // 修改文案
        'merge' // 处理合并分支
      ],
    ],
  },
};

Commitizen & cz-conventional-changelog-zh

commitizen: 基于Node.js的 git commit 命令行工具,辅助生成标准化规范化的 commit message。官方文档
cz-conventional-changelog-zh:基于cz-conventional-changelog的基础修改的中文版

Commitizen &cz-conventional-changelog-zh安装

修改 package.json 指定使用的适配器

  "config": {
    "commitizen": {
      "path": "node_modules/cz-conventional-changelog-zh",
      "types": {
        "feat": {
          "description": "一个新功能",
          "title": "Features"
        },
        "fix": {
          "description": "一个bug",
          "title": "Bug Fixes"
        },
        "docs": {
          "description": "文档增删改",
          "title": "Documentation"
        },
        "text": {
          "description": "修改文案",
          "title": "Text"
        },
        "merge": {
          "description": "处理合并分支",
          "title": "Merges"
        },
        "style": {
          "description": "样式修改(空白、格式、缺少分号等)",
          "title": "Styles"
        },
        "refactor": {
          "description": "既不修复bug也不添加新功能的更改",
          "title": "Code Refactoring"
        },
        "perf": {
          "description": "性能优化",
          "title": "Performance Improvements"
        },
        "test": {
          "description": "增加测试",
          "title": "Tests"
        },
        "build": {
          "description": "影响构建系统或外部依赖项的更改(示例范围:gulp、broccoli、npm)",
          "title": "Builds"
        },
        "ci": {
          "description": "对CI配置文件和脚本的更改(示例范围:Travis, Circle, BrowserStack, SauceLabs)",
          "title": "Continuous Integrations"
        },
        "chore": {
          "description": "除src目录或测试文件以外的修改",
          "title": "Chores"
        },
        "revert": {
          "description": "回退历史版本",
          "title": "Reverts"
        },
        "conflict": {
          "description": "修改冲突",
          "title": "Conflict"
        },
        "font": {
          "description": "字体文件更新",
          "title": "Fonts"
        },
        "delete": {
          "description": "删除文件",
          "title": "Delete Files"
        },
        "stash": {
          "description": "暂存文件",
          "title": "Stash Files"
        }
      }
    }
  }

添加提交指令

 "scripts": {
     "commit": "cz"
 }

执行 commit 指令进行代码提交流程,执行前需将改动的文件通过 git add 添加到暂存区

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值