index.mjs 4.5 KB

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