使用husky配置git代码提交规范

1、安装husky

yarn add --dev husky
   
   
    
    

2、配置husky

在 package.json 中的 script 中添加一条prepare命令:


   
   
    
    
  1. "scripts": {
  2. "serve": "vue-cli-service serve",
  3. "build": "vue-cli-service build",
  4. "lint": "vue-cli-service lint",
  5. "prepare": "husky install"
  6. },

执行以下命令进行husky初始化,自动生成.husky文件夹

yarn prepare

   
   
    
    

3、代码规范配置

  • 安装 @commitlint/config-conventional 和 @commitlint/cli
yarn add @commitlint/config-conventional @commitlint/cli -D
   
   
    
    
  • 配置 commitlint,在项目根目录下新建 commitlint.config.js 文件

   
   
    
    
  1. module. exports = {
  2. ignores: [ (commit) => commit. includes( 'init')],
  3. extends: [ '@commitlint/config-conventional'],
  4. rules: {
  5. 'body-leading-blank': [ 2, 'always'], // body 开始于空白行
  6. 'footer-leading-blank': [ 1, 'always'],
  7. 'header-max-length': [ 2, 'always', 108], // header 字符最大长度为 108
  8. 'subject-empty': [ 2, 'never'], // subject 不为空
  9. 'type-empty': [ 2, 'never'], // type 不为空
  10. 'subject-case': [ 0],
  11. 'type-enum': [
  12. 2,
  13. 'always',
  14. [
  15. 'feat', // 增加新功能
  16. 'fix', // 修复问题/BUG
  17. 'perf', // 优化/性能提升
  18. 'style', // 代码风格相关无影响运行结果的
  19. 'docs', // 文档/注释
  20. 'test', // 测试相关
  21. 'refactor', // 重构
  22. 'build', // 对构建系统或者外部依赖项进行了修改
  23. 'ci', // 对 CI 配置文件或脚本进行了修改
  24. 'chore', // 依赖更新/脚手架配置修改等
  25. 'revert', // 撤销修改
  26. 'wip', // 开发中
  27. 'workflow', // 工作流改进
  28. 'types', // 类型修改
  29. 'release',
  30. ],
  31. ],
  32. },
  33. };
  • 使用 husky 生成 commit-msg 文件,验证提交信息
npx husky add .husky/commit-msg "npx --no-install commitlint --edit "$1""
   
   
    
    

commit-msg文件内容如下: 


   
   
    
    
  1. #! /usr/bin/env sh
  2. . "$(dirname -- "$0 ")/_/husky.sh"
  3. npx --no-install commitlint --edit "$1"

新时代农民工 

1、安装husky

yarn add --dev husky
   
   
  
  

2、配置husky

在 package.json 中的 script 中添加一条prepare命令:


   
   
  
  
  1. "scripts": {
  2. "serve": "vue-cli-service serve",
  3. "build": "vue-cli-service build",
  4. "lint": "vue-cli-service lint",
  5. "prepare": "husky install"
  6. },

执行以下命令进行husky初始化,自动生成.husky文件夹

yarn prepare

   
   
  
  

3、代码规范配置

  • 安装 @commitlint/config-conventional 和 @commitlint/cli
yarn add @commitlint/config-conventional @commitlint/cli -D
   
   
  
  
  • 配置 commitlint,在项目根目录下新建 commitlint.config.js 文件

   
   
  
  
  1. module. exports = {
  2. ignores: [ (commit) => commit. includes( 'init')],
  3. extends: [ '@commitlint/config-conventional'],
  4. rules: {
  5. 'body-leading-blank': [ 2, 'always'], // body 开始于空白行
  6. 'footer-leading-blank': [ 1, 'always'],
  7. 'header-max-length': [ 2, 'always', 108], // header 字符最大长度为 108
  8. 'subject-empty': [ 2, 'never'], // subject 不为空
  9. 'type-empty': [ 2, 'never'], // type 不为空
  10. 'subject-case': [ 0],
  11. 'type-enum': [
  12. 2,
  13. 'always',
  14. [
  15. 'feat', // 增加新功能
  16. 'fix', // 修复问题/BUG
  17. 'perf', // 优化/性能提升
  18. 'style', // 代码风格相关无影响运行结果的
  19. 'docs', // 文档/注释
  20. 'test', // 测试相关
  21. 'refactor', // 重构
  22. 'build', // 对构建系统或者外部依赖项进行了修改
  23. 'ci', // 对 CI 配置文件或脚本进行了修改
  24. 'chore', // 依赖更新/脚手架配置修改等
  25. 'revert', // 撤销修改
  26. 'wip', // 开发中
  27. 'workflow', // 工作流改进
  28. 'types', // 类型修改
  29. 'release',
  30. ],
  31. ],
  32. },
  33. };
  • 使用 husky 生成 commit-msg 文件,验证提交信息
npx husky add .husky/commit-msg "npx --no-install commitlint --edit "$1""
   
   
  
  

commit-msg文件内容如下: 


   
   
  
  
  1. #! /usr/bin/env sh
  2. . "$(dirname -- "$0 ")/_/husky.sh"
  3. npx --no-install commitlint --edit "$1"

新时代农民工 

猜你喜欢

转载自blog.csdn.net/weixin_64310738/article/details/129034136