index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import type { UserConfig } from 'cz-git';
  2. import { execSync } from 'node:child_process';
  3. import { getPackagesSync } from '@vben/node-utils';
  4. const { packages } = getPackagesSync();
  5. const allowedScopes = [
  6. ...packages.map((pkg) => pkg.packageJson.name),
  7. 'project',
  8. 'style',
  9. 'lint',
  10. 'ci',
  11. 'dev',
  12. 'deploy',
  13. 'other',
  14. ];
  15. // precomputed scope
  16. const scopeComplete = execSync('git status --porcelain || true')
  17. .toString()
  18. .trim()
  19. .split('\n')
  20. .find((r) => ~r.indexOf('M src'))
  21. ?.replace(/(\/)/g, '%%')
  22. ?.match(/src%%((\w|-)*)/)?.[1]
  23. ?.replace(/s$/, '');
  24. const userConfig: UserConfig = {
  25. extends: ['@commitlint/config-conventional'],
  26. plugins: ['commitlint-plugin-function-rules'],
  27. prompt: {
  28. /** @use `pnpm commit :f` */
  29. alias: {
  30. b: 'build: bump dependencies',
  31. c: 'chore: update config',
  32. f: 'docs: fix typos',
  33. r: 'docs: update README',
  34. s: 'style: update code format',
  35. },
  36. allowCustomIssuePrefixs: false,
  37. // scopes: [...scopes, 'mock'],
  38. allowEmptyIssuePrefixs: false,
  39. customScopesAlign: scopeComplete ? 'bottom' : 'top',
  40. defaultScope: scopeComplete,
  41. // English
  42. typesAppend: [
  43. { name: 'wip: work in process', value: 'wip' },
  44. { name: 'workflow: workflow improvements', value: 'workflow' },
  45. { name: 'types: type definition file changes', value: 'types' },
  46. ],
  47. // 中英文对照版
  48. // messages: {
  49. // type: '选择你要提交的类型 :',
  50. // scope: '选择一个提交范围 (可选):',
  51. // customScope: '请输入自定义的提交范围 :',
  52. // subject: '填写简短精炼的变更描述 :\n',
  53. // body: '填写更加详细的变更描述 (可选)。使用 "|" 换行 :\n',
  54. // breaking: '列举非兼容性重大的变更 (可选)。使用 "|" 换行 :\n',
  55. // footerPrefixsSelect: '选择关联issue前缀 (可选):',
  56. // customFooterPrefixs: '输入自定义issue前缀 :',
  57. // footer: '列举关联issue (可选) 例如: #31, #I3244 :\n',
  58. // confirmCommit: '是否提交或修改commit ?',
  59. // },
  60. // types: [
  61. // { value: 'feat', name: 'feat: 新增功能' },
  62. // { value: 'fix', name: 'fix: 修复缺陷' },
  63. // { value: 'docs', name: 'docs: 文档变更' },
  64. // { value: 'style', name: 'style: 代码格式' },
  65. // { value: 'refactor', name: 'refactor: 代码重构' },
  66. // { value: 'perf', name: 'perf: 性能优化' },
  67. // { value: 'test', name: 'test: 添加疏漏测试或已有测试改动' },
  68. // { value: 'build', name: 'build: 构建流程、外部依赖变更 (如升级 npm 包、修改打包配置等)' },
  69. // { value: 'ci', name: 'ci: 修改 CI 配置、脚本' },
  70. // { value: 'revert', name: 'revert: 回滚 commit' },
  71. // { value: 'chore', name: 'chore: 对构建过程或辅助工具和库的更改 (不影响源文件、测试用例)' },
  72. // { value: 'wip', name: 'wip: 正在开发中' },
  73. // { value: 'workflow', name: 'workflow: 工作流程改进' },
  74. // { value: 'types', name: 'types: 类型定义文件修改' },
  75. // ],
  76. // emptyScopesAlias: 'empty: 不填写',
  77. // customScopesAlias: 'custom: 自定义',
  78. },
  79. rules: {
  80. /**
  81. * type[scope]: [function] description
  82. *
  83. * ^^^^^^^^^^^^^^ empty line.
  84. * - Something here
  85. */
  86. 'body-leading-blank': [2, 'always'],
  87. /**
  88. * type[scope]: [function] description
  89. *
  90. * - something here
  91. *
  92. * ^^^^^^^^^^^^^^
  93. */
  94. 'footer-leading-blank': [1, 'always'],
  95. /**
  96. * type[scope]: [function] description
  97. * ^^^^^
  98. */
  99. 'function-rules/scope-enum': [
  100. 2, // level: error
  101. 'always',
  102. (parsed: { scope: string }) => {
  103. if (!parsed.scope || allowedScopes.includes(parsed.scope)) {
  104. return [true];
  105. }
  106. return [false, `scope must be one of ${allowedScopes.join(', ')}`];
  107. },
  108. ],
  109. /**
  110. * type[scope]: [function] description [No more than 108 characters]
  111. * ^^^^^
  112. */
  113. 'header-max-length': [2, 'always', 108],
  114. 'scope-enum': [0],
  115. 'subject-case': [0],
  116. 'subject-empty': [2, 'never'],
  117. 'type-empty': [2, 'never'],
  118. /**
  119. * type[scope]: [function] description
  120. * ^^^^
  121. */
  122. 'type-enum': [
  123. 2,
  124. 'always',
  125. [
  126. 'feat',
  127. 'fix',
  128. 'perf',
  129. 'style',
  130. 'docs',
  131. 'test',
  132. 'refactor',
  133. 'build',
  134. 'ci',
  135. 'chore',
  136. 'revert',
  137. 'types',
  138. 'release',
  139. 'improvement',
  140. ],
  141. ],
  142. },
  143. };
  144. export default userConfig;