欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > 关于husky8.0 与 4.0的配置

关于husky8.0 与 4.0的配置

2025/6/14 14:56:40 来源:https://blog.csdn.net/xy_peng/article/details/145077596  浏览:    关键词:关于husky8.0 与 4.0的配置

husky的场景使用很多,一般大多场景是在配置git commit 命令拦截hook, 校验 commit-msg 格式规范。以下环境默认:git >= 2.27.0, node >=14

1、安装huskey@8.0.1

npm install --save-dev husky@8.0.1

2、初始化配置文件

  • 在package.json scripts 属性中添加命令并保存:

"scripts": {"prepare": "npx husky install"
}
  •  在控制台输入:npm run prepare 初始化 husky,  则在工程根目录创建.husky目录  ,如下图:

  •  初始化commit-msg函数配置文件
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

        在./husky/commit-msg文件中生成

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx --no-install commitlint --edit $1
  • 初始化pre-commit函数配置文件
npx husky add .husky/pre-commit "npx lint-staged"
  • 在./husky/pre-commit文件中生成  
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged

以上: 钩子函数已经配置完毕, 执行 git commit 命令之后,会被拦截开始执行./husky/下 pre-commit、commit-msg 配置文件中脚本  

3、配置自定义的git commit-msg格式规范

  • commitlint 提交校验安装配置

    npm install --save-dev @commitlint/cli@17.0.2 @commitlint/config-conventional@17.0.2
  • 在工程目录创建commitlint.config.js文件

'use strict';
module.exports = {extends: ['@commitlint/config-conventional'],rules: {'type-enum': [2,'always',['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'chore', 'revert', 'build', "impr", "ci", "jvm", "pom", "apm", "conf", "typo", "wip", ]],'type-case': [0],'type-empty': [0],'scope-empty': [0],'scope-case': [0],'subject-full-stop': [0, 'never'],'subject-case': [0, 'never'],'header-max-length': [0, 'always', 72]}
};

 以下是4.0的配置

4、安装huskey@4.3.8

npm install --save-dev husky@4.3.8

5、初始化配置文件

- 在package.json scripts 属性中添加命令并保存:

{"husky": {"hooks": {"pre-commit": "lint-staged","commit-msg": "commitlint -E HUSKY_GIT_PARAMS"}},
}

6、配置自定义的git commit-msg格式规范

- commitlint 提交校验安装配置

 

npm install --save-dev @commitlint/cli@16.3.0 @commitlint/config-conventional@16.2.4

注意:高版本与低版本的husky有冲突

请检查根目录下.git/config 文件中是否含有高版本husky的字段配置, 若有则注释掉husky的配置

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词