Commitlint
安装
bash
pnpm add -D @uniboot/commitlint-config @commitlint/cli包内已带 @commitlint/config-conventional、commitlint-plugin-function-rules、cz-git、czg、@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-git 的 UserConfig 对象,可直接被 @commitlint/cli 与 cz-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)plugins:commitlint-plugin-function-rules,用于编程化的function-rules/scope-enumtype-enum:feat/fix/perf/style/docs/test/refactor/build/ci/chore/revert/types/releasescope:通过@uniboot/node-utils的getPackagesSync()在 monorepo 下读取所有子包的name作为允许的 scope,并补充project/style/lint/ci/dev/deploy/otherheader-max-length:108prompt(cz-git):- 类型扩展:追加
workflow、types - 别名:
b→build: bump dependencies、c→chore: update config、f→docs: fix typos、r→docs: update README、s→style: update code format defaultScope:根据git status --porcelain中M 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 :f≈docs: fix typos(依赖cz-git的alias)。 - 接入 husky:在
.husky/commit-msg中调用npx --no -- commitlint --edit "$1",与本预设一起即可启用规则与函数级 scope 校验。
在单包仓库中使用
function-rules/scope-enum 在单包仓库下也能工作:getPackagesSync() 会返回当前包,加上额外 scope 白名单(project、style、lint、ci、dev、deploy、other)通常已够用。如果团队希望 scope 走「枚举常量」而非「monorepo 包名」,可覆盖 rules['function-rules/scope-enum'] 后自行实现。