代码提交检查husky+Lint-staged+Commitizen & cz-git

注意一定要使用pnpm来下载所有的husky包,不然到后面无法生成commit-msg钩子

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

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

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

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


一、husky

Husky 是 Git 钩子工具,可以设置在 git 各个阶段(pre-commit、commit-msg 等)触发。

官网https://typicode.github.io/husky 推荐安装指令

1. 前提条件 项目有.git 如果没有需要生成

git init

2 .自动配置husky

npx husky-init

3. 安装husky 执行pnpm i

pnpm i

二、Lint-staged 增量检测提交代码

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

1 .安装

pnpm i lint-staged -D

2 .package.json 中添加不同文件在 git 提交执行的 lint 检测配置

// package.json
{
    //...
     "lint-staged": {
        "*.{js,ts}": [
          "eslint --fix",
          "prettier --write"
        ],
        "*.{cjs,json}": [
          "prettier --write"
        ],
        "*.{vue,html}": [
          "eslint --fix",
          "prettier --write",
          "stylelint --fix --allow-empty-input"
        ],
        "*.{scss,css}": [
          "stylelint --fix --allow-empty-input",
          "prettier --write"
        ]
      }
}

3. package.json添加指令

"scripts": {
   	// ...
    "lint:lint-staged": "lint-staged",
  },
  • 4 .husky/pre-commit修改提交前钩子命令
#npm test
npm run lint:lint-staged --allow-empty

现在运行git commit 会执行 lint:lint-staged脚本

然后执行

 "lint-staged": {
        "*.{js,ts}": [
          "eslint --fix",
          "prettier --write"
        ],
        "*.{cjs,json}": [
          "prettier --write"
        ],
        "*.{vue,html}": [
          "eslint --fix",
          "prettier --write",
          "stylelint --fix --allow-empty-input"
        ],
        "*.{scss,css}": [
          "stylelint --fix --allow-empty-input",
          "prettier --write"
        ]
      }

中的所有脚本对所有文件进行fix

三、Commitlint

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

1. 安装

pnpm i @commitlint/cli @commitlint/config-conventional -D

2 根目录创建 commitlint.config.js 配置文件

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

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

3 执行下面命令生成 commint-msg 钩子用于 git 提交信息校验

旧版本

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

新版本

echo "npx --no -- commitlint --edit \$1" > .husky/commit-msg

生成commit-msg

进行提交测试

注意格式 git commit -m "type: 空格+msg"


四、 Commitizen & cz-git 【额外提交规范 非必要】

commitizen: 基于Node.js的 git commit 命令行工具,辅助生成标准化规范化的 commit message。–官方文档

cz-git: 一款工程性更强,轻量级,高度自定义,标准输出格式的 commitizen 适配器。–官方文档

1.安装

pnpm i commitizen cz-git -D

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

{
    // ...
     "config": {
        "commitizen": {
          "path": "node_modules/cz-git"
        }
      }
}

3.修改 commitlint.config.js

/* eslint-env node */
module.exports = {
  // 继承的规则
  extends: ['@commitlint/config-conventional'],
  // 自定义规则
  rules: {
    // @see https://commitlint.js.org/#/reference-rules

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

  prompt: {
    messages: {
      type: '选择你要提交的类型 :',
      scope: '选择一个提交范围(可选):',
      customScope: '请输入自定义的提交范围 :',
      subject: '填写简短精炼的变更描述 :\n',
      body: '填写更加详细的变更描述(可选)。使用 "|" 换行 :\n',
      breaking: '列举非兼容性重大的变更(可选)。使用 "|" 换行 :\n',
      footerPrefixesSelect: '选择关联issue前缀(可选):',
      customFooterPrefix: '输入自定义issue前缀 :',
      footer: '列举关联issue (可选) 例如: #31, #I3244 :\n',
      generatingByAI: '正在通过 AI 生成你的提交简短描述...',
      generatedSelectByAI: '选择一个 AI 生成的简短描述:',
      confirmCommit: '是否提交或修改commit ?'
    },
    // prettier-ignore
    types: [
      { value: "feat",     name: "特性:     ✨  新增功能", emoji: ":sparkles:" },
      { value: "fix",      name: "修复:     🐛  修复缺陷", emoji: ":bug:" },
      { value: "docs",     name: "文档:     📝  文档变更", emoji: ":memo:" },
      { value: "style",    name: "格式:     🌈  代码格式(不影响功能,例如空格、分号等格式修正)", emoji: ":lipstick:" },
      { value: "refactor", name: "重构:     🔄  代码重构(不包括 bug 修复、功能新增)", emoji: ":recycle:" },
      { value: "perf",     name: "性能:     🚀  性能优化", emoji: ":zap:" },
      { value: "test",     name: "测试:     🧪  添加疏漏测试或已有测试改动", emoji: ":white_check_mark:"},
      { value: "build",    name: "构建:     📦️  构建流程、外部依赖变更(如升级 npm 包、修改 vite 配置等)", emoji: ":package:"},
      { value: "ci",       name: "集成:     ⚙️  修改 CI 配置、脚本",  emoji: ":ferris_wheel:"},
      { value: "revert",   name: "回退:     ↩️  回滚 commit",emoji: ":rewind:"},
      { value: "chore",    name: "其他:     🛠️  对构建过程或辅助工具和库的更改(不影响源文件、测试用例)", emoji: ":hammer:"},
    ],
    useEmoji: true,
    emojiAlign: 'center',
    useAI: false,
    aiNumber: 1,
    themeColorCode: '',
    scopes: [],
    allowCustomScopes: true,
    allowEmptyScopes: true,
    customScopesAlign: 'bottom',
    customScopesAlias: 'custom',
    emptyScopesAlias: 'empty',
    upperCaseSubject: false,
    markBreakingChangeMode: false,
    allowBreakingChanges: ['feat', 'fix'],
    breaklineNumber: 100,
    breaklineChar: '|',
    skipQuestions: [],
    issuePrefixes: [{ value: 'closed', name: 'closed:   ISSUES has been processed' }],
    customIssuePrefixAlign: 'top',
    emptyIssuePrefixAlias: 'skip',
    customIssuePrefixAlias: 'custom',
    allowCustomIssuePrefix: true,
    allowEmptyIssuePrefix: true,
    confirmColorize: true,
    maxHeaderLength: Infinity,
    maxSubjectLength: Infinity,
    minSubjectLength: 0,
    scopeOverrides: undefined,
    defaultBody: '',
    defaultIssues: '',
    defaultScope: '',
    defaultSubject: ''
  }
}

4.package.json 添加 commit 指令

  "scripts": {
  	// ...
    "commit": "git-cz"
  },

5.进行代码提交

// 代码添加暂存区
git add . 
// 验证
pnpm commit

可以看到git-cz配置完成

可以便捷的填写提交信息

并且同样可以执行husky的pre-commit钩子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值