Commitlint

安装

bash
pnpm add -D @uniboot/commitlint-config @commitlint/cli

包内已带 @commitlint/config-conventionalcommitlint-plugin-function-rulescz-gitczg@uniboot/node-utils 等运行时依赖;@commitlint/cli 由项目安装。

使用

在仓库根目录放 commitlint.config.mjs(或 commitlint.config.js,需 ESM):

js
// commitlint.config.mjs
import config from '@uniboot/commitlint-config'

export default config

预设导出的是 cz-gitUserConfig 对象,可直接被 @commitlint/clicz-git / czg 读取;需要按团队规范覆盖时,在导出对象上展开后再写自己的键即可:

js
import baseConfig from '@uniboot/commitlint-config'

export default {
  ...baseConfig,
  rules: {
    ...baseConfig.rules,
    'header-max-length': [2, 'always', 120],
  },
}

预设里大约包含什么

  • extends@commitlint/config-conventional(基于 conventional commits)
  • pluginscommitlint-plugin-function-rules,用于编程化的 function-rules/scope-enum
  • type-enumfeat / fix / perf / style / docs / test / refactor / build / ci / chore / revert / types / release
  • scope:通过 @uniboot/node-utilsgetPackagesSync() 在 monorepo 下读取所有子包的 name 作为允许的 scope,并补充 project / style / lint / ci / dev / deploy / other
  • header-max-length108
  • promptcz-git):
    • 类型扩展:追加 workflowtypes
    • 别名:bbuild: bump dependenciescchore: update configfdocs: fix typosrdocs: update READMEsstyle: update code format
    • defaultScope:根据 git status --porcelainM src/<feature> 的路径做猜测填充

完整内容以源码 uniboot-configs/packages/lint-configs/commitlint-config/index.mjs 为准。

与 czg / husky 的协作

  • 使用 czg 进行交互式提交:pnpm dlx czg(或在 scripts 中加入 "commit": "czg"),可读取 commitlint.config.mjs 中的 prompt 配置。
  • 别名提交:pnpm commit :fdocs: fix typos(依赖 cz-gitalias)。
  • 接入 husky:在 .husky/commit-msg 中调用 npx --no -- commitlint --edit "$1",与本预设一起即可启用规则与函数级 scope 校验。

在单包仓库中使用

function-rules/scope-enum 在单包仓库下也能工作:getPackagesSync() 会返回当前包,加上额外 scope 白名单(projectstylelintcidevdeployother)通常已够用。如果团队希望 scope 走「枚举常量」而非「monorepo 包名」,可覆盖 rules['function-rules/scope-enum'] 后自行实现。