前端husky中commitlint配置

最近想在项目使用husky做一些commit限制,发现一直加不上,于是查询文档发现,新版本husky和旧版本的husky添加钩子的做法是不一样的。

这里直接说一下新版本husky如何操作:

  1. 安装husky
npm install husky --save-dev
  1. 启动husky
npx husky install
  1. 在package.json中添加一个指令,这样就可以在安装husky之后自动启用预设的钩子了。
{
  "scripts": {
    "prepare": "husky install"
  }
}

下面我们添加一个git钩子,以commitlint为例子:

  1. 先写一下配置文件,在package.json同级目录下新建commitlint.config.js,这里贴上一个简单的配置:
module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'body-leading-blank': [2, 'always'],
    'footer-leading-blank': [1, 'always'],
    'header-max-length': [2, 'always', 108],
    'subject-empty': [2, 'never'],
    'type-empty': [2, 'never'],
    'subject-case': [0],
    'type-enum': [2, 'always', ['feat', 'fix', 'perf', 'style', 'docs', 'test', 'refactor', 'build', 'ci', 'chore', 'revert', 'wip', 'workflow', 'types', 'release']],
  },
};
// { value: 'feat', name: 'feat:     新增功能feature' },
// { value: 'fix', name: 'fix:      修复bug缺陷' },
// { value: 'types', name: 'types:    类型修改' },
// { value: 'docs', name: 'docs:     仅仅修改了文档变更,比如readme,changelog等' },
// { value: 'perf', name: 'perf:     性能优化' },
// { value: 'style', name: 'style:    代码格式(不影响功能,例如空格、分号等格式修正)' },
// { value: 'refactor', name: 'refactor: 代码重构(不包括 bug 修复、功能新增)' },
// { value: 'test', name: 'test:     添加、修改测试用例' },
// { value: 'build', name: 'build:    构建流程、外部依赖变更(如升级 npm 包、修改 webpack 配置等)' },
// { value: 'ci', name: 'ci:       修改 CI 配置、脚本' },
// { value: 'revert', name: 'revert:   回滚 commit' },
// { value: 'wip', name: 'wip:      功能开发中' },
// { value: 'workflow', name: 'workflow: 工作流程变更' },
// { value: 'chore', name: 'chore:    对构建过程或辅助工具和库的更改(不影响源文件、测试用例)' },
// { value: 'revert', name: 'revert:    回滚到上一个版本' },
  1. 安装commitlint相关依赖
npm install --save-dev @commitlint/config-conventional @commitlint/cli
  1. 在husky中添加一个commit-msg钩子:
    文档里面给出了windows的注意点:npx husky add …可以不好用,那么请使用node node_modules/.bin/husky add …,所以配置如下:
 node node_modules/husky/lib/bin add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'

这样就可以在我们每次执行git commit -m "XXX"的时候就可以校验我们这次commit的格式是否符合规范了。

你可能感兴趣的:(husky和它的钩子们,前端)