前面已经完成了 prettiereslint ,还有pre-commit的时候 扫描代码并格式化的功能
接下来就是处理怎样控制commit提交信息规范的问题

安装插件

  1. yarn add @commitlint/cli @commitlint/config-conventional --dev

配置 commit-msg hooks

  1. yarn husky add .husky/commit-msg 'yarn commitlint --edit "$1"'
  2. or:
  3. npx husky add .husky/commit-msg 'npx --no commitlint --edit "$1"'

效果:
在commit的时候 执行步骤

  1. 执行pre-commit 钩子进行代码格式校验
  2. 执行 commit-msg 钩子,进行提交信息的校验 ```json

    !/usr/bin/env sh

    . “$(dirname — “$0”)/_/husky.sh”

yarn commitlint —edit $1

  1. <a name="kVcvn"></a>
  2. ## 配置commitlint.config.js
  3. ```json
  4. const checkType = (header) => {
  5. header = `${header}`;
  6. const enumType = ['feat', 'fix', 'style', 'chore', 'test', 'ci', 'refactor', 'revert', 'reformat', 'docs'];
  7. const realType = header.split(':')[0];
  8. return enumType.includes(realType);
  9. };
  10. const checkSubject = (header) => {
  11. header = `${header}`;
  12. const realSubject = header.split(':')[1];
  13. if (!realSubject) {
  14. return false;
  15. }
  16. return realSubject.length > 0;
  17. };
  18. module.exports = {
  19. extends: ['@commitlint/config-conventional'],
  20. rules: {
  21. // 'body-leading-blank': [2, 'always'], // body换行
  22. // 'header-max-length': [2, 'never', 72], // header 最长72
  23. 'type-enum-rule': [2, 'never'],
  24. 'subject-enum-rule': [2, 'never'],
  25. 'type-enum': [0, 'never'],
  26. 'type-empty': [0, 'always'],
  27. 'subject-empty': [0, 'always'],
  28. },
  29. plugins: [
  30. {
  31. rules: {
  32. 'type-enum-rule': ({ header }) => {
  33. return [
  34. checkType(header),
  35. '需要包含提交类型,格式如: "feat: 开发新功能" 中的feat, ' +
  36. '可选值有: feat/fix/style/test/chore/ci/..., 类型后面紧跟英文冒号分隔主题信息',
  37. ];
  38. },
  39. 'subject-enum-rule': ({ header }) => {
  40. return [checkSubject(header), '需要包含提交主题, 格式如: "feat: 开发新功能" 中的 开发新功能'];
  41. },
  42. },
  43. },
  44. ],
  45. };

测试效果

  1. git add .
  2. git commit -m 'xxx'
  3. git commit -m 'test:'
  4. git commit -m 'test: 测试'

image.png