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

╰半夏微凉° 2024-03-31 16:01 205阅读 0赞

1、安装husky

  1. 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文件夹

  1. yarn prepare

3、代码规范配置

  • 安装 @commitlint/config-conventional 和 @commitlint/cli

    yarn add @commitlint/config-conventional @commitlint/cli -D

  • 配置 commitlint,在项目根目录下新建 commitlint.config.js 文件

    module.exports = {
    ignores: [(commit) => commit.includes(‘init’)],
    extends: [‘@commitlint/config-conventional’],
    rules: {

    1. 'body-leading-blank': [2, 'always'], // body 开始于空白行
    2. 'footer-leading-blank': [1, 'always'],
    3. 'header-max-length': [2, 'always', 108], // header 字符最大长度为 108
    4. 'subject-empty': [2, 'never'], // subject 不为空
    5. 'type-empty': [2, 'never'], // type 不为空
    6. 'subject-case': [0],
    7. 'type-enum': [
    8. 2,
    9. 'always',
    10. [
    11. 'feat', // 增加新功能
    12. 'fix', // 修复问题/BUG
    13. 'perf', // 优化/性能提升
    14. 'style', // 代码风格相关无影响运行结果的
    15. 'docs', // 文档/注释
    16. 'test', // 测试相关
    17. 'refactor', // 重构
    18. 'build', // 对构建系统或者外部依赖项进行了修改
    19. 'ci', // 对 CI 配置文件或脚本进行了修改
    20. 'chore', // 依赖更新/脚手架配置修改等
    21. 'revert', // 撤销修改
    22. 'wip', // 开发中
    23. 'workflow', // 工作流改进
    24. 'types', // 类型修改
    25. 'release',
    26. ],
    27. ],

    },
    };

  • 使用 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"

新时代农民工

发表评论

表情:
评论列表 (有 0 条评论,205人围观)

还没有评论,来说两句吧...

相关阅读

    相关 git commit 代码提交规范

    我们在代码提交时 git 的 commit 信息是很重要的,但是每次写的时候总想偷懒怎么办,咱们可以制定个规范必须按照某种格式提交注释。 法一:设置 git commit

    相关 git提交规范

    Commit message 的作用 比如,下面的命令显示上次发布后的变动,每个commit占据一行。你只看行首,就知道某次 commit 的目的。 提供更多的历