配置 `git hooks` 安装 husky@7 钩子+`lint-staged@12 格式化缓冲区的内容

本文介绍如何配置githooks工具Husky与代码格式化工具Lint-Staged,包括解决Windows环境下Husky不生效的问题,以及如何通过Commitizen规范commit message。

问题先行

  • git hook husky 不生效问题
    • v4 版本及以上 在 package.json 中配置 husky 字段已经不可用。Why husky has dropped conventional JS config 放弃了传统的js配置。
    • 如果您使用Windows,那么husky只会使用系统上全局安装的版本。这里需要在全局安装 husky

配置 git hooks 安装 husky@7 钩子+lint-staged@12 格式化缓冲区的内容

  1. git 版本 > 2.9.0

  2. npm install husky --save-dev 在windows下我遇到不生效的问题,有资料显示,windows下用的是全局 husky,需要全局安装,或者 执行node_modules下的 husky 官方有资料,没仔细看。

    • For Windows users, if you see the help message when running npx husky add ..., try node node_modules/.bin/husky add ... instead. This isn’t an issue with husky code.

    1. 启用 git hooks npx husky install,默认会在项目根目录下初始化一个 .husky 目录
    2. 创建钩子
    npx husky add .husky/pre-commit "npm test"
    git add .husky/pre-commit
    
    1. try a commit
    git commit -m "Keep clam and commit"
    
    1. hook 可以放到专职配置文件中, create a config file .huskyrc.json in root folder。
      设置每次hook对应触发执行的脚本。
    {
        "hook": {
            // "pre-commit": "npm test"
            "pre-commit": "lint-staged"
        }
    }
    

    或者 yaml 格式配置 .huskyrc.yml

    hooks:
      pre-commit: 'npm test'
    
  3. npm install lint-staged -D

    1. 创建配置文件 .lintstagedrc.json
    {
      "*.{js,mjs,wxml,json,md}": ["prettier -c --write", "git add"],
      "*.{scss, wxss}": ["stylelint --syntax=scss --fix", "git add"]
    }
    

    或者 yaml 格式配置 .linstagedrc.yml

    '*.{js,vue,html,less,json,md,yml,yaml}':
      - prettier -c --write
    
  4. 更新 husky 配置,仅对缓存内容执行 hooks 操作

    #let pre-commit hook call lint-staged
    npx husky set .husky/pre-commit "npx lint-staged"
    

    也可以直接 修改 .husky/pre-commit 文件

  5. 尝试 commit,验证 hooks 是否生效

规范 commit message 提交

  1. global install Commitizen 是一个帮助撰写规范 commit message 的工具。他有一个命令行工具 cz-cli
// 全局安装 Commitizen
npm install -g commitizen

全局安装 Commitizen 后,用 cz-conventional-changelog 适配器来初始化你的项目

// 初始化 cz-conventional-changelog 适配器
commitizen init cz-conventional-changelog --save-dev --save-exact

初始化做了3件事

  • 安装 cz-conventional-changelog 依赖
  • 把依赖保存到 package.json 的 dependencies 或 devDependencies 中
  • 在根目录的 package.json 中 添加如下所示的 config.commitizen

或者,在项目根目录新建一个 .czrc

{
  "path": "cz-conventional-changelog"
}

执行 git cz 效果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-epi17mLv-1653551271206)(https://note.youdao.com/yws/res/e/WEBRESOURCE60e19bc45816f5c4a7366e4f2fa0cefe)]

commitlint 校验提交

参考
commitlint 可以检查 commit messages 是否符合常规提交格式,需要一份校验配置,推荐@commitlint/config-conventional 。

  1. install commitlint @commitlint/config-conventional
npm i --save-dev @commitlint/config-conventional @commitlint/cli
  1. 项目根目录创建 commitlint.config.js
module.exports = {
  extends: ["@commitlint/config-conventional"],
  // rules 里面可以设置一些自定义的校验规则
  rules: {},
};

注意:因为 @commitlint/config-conventional 校验规则遵循 Angular 的规范, 所以我们在用 cz-customizable 自定义中文配置时, 是按照给出的符合 Angular 规范的示例 cz-config-EXAMPLE.js 编写.cz-config.js 的。但是如果你自定义的 Commitizen 配置不符合 Angular 规范,可以使用 commitlint-config-cz 设置校验规则。(推荐还是按照 Angular 规范进行 cz-customizable 自定义配置)

一个项目应该有的配置文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CMSPcfuj-1653551271208)(https://note.youdao.com/yws/res/d/WEBRESOURCEbfac340103a0f804ebf737bd38a5423d)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值