Eson Wong's Blog

编程笔记,投资记录, 读书总结, 生活心得

0%

Commitlint 使用入门

Commitlint 是一个用于检查 Git 提交的 message 是否符合项目的规范的工具。规范的提交内容可以让开发者方便快速浏览查找比较代码,可以直接从 提交内容 生成 Change log,触发 CI 流程。

1
echo '提交内容' | npx commitlint

命令行工具

安装

全局安装 @commitlint/cli

1
2
3
4
5
npm install -g @commitlint/cli

# or

yarn global add @commitlint/cli

配置规则

@commitlint/config-conventional 包含了 约定式提交 的规则。

安装

1
2
3
4
5
npm install -g @commitlint/config-conventional

# or

yarn global add @commitlint/config-conventional

创建 commitlint.config.js 文件来配置

1
2
3
4
// commitlint.config.js
module.exports = {
extends: ["@commitlint/config-conventional"],
};

使用 commitlint 命令来测试 commit message

运行 echo '提交内容' | commitlint 来测试 commit message。commitlint 命令会打印规范检查信息和返回状态码。

1
2
3
4
5
6
7
# Lint from stdin
echo 'foo: bar' | commitlint
# ⧗ input: foo: bar
#type must be one of [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test] [type-enum]

# ✖ found 1 problems, 0 warnings
# ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

npx 使用

1
npx @commitlint/cli

配合 Husky 使用

我写了一篇 Husky 入门教程 博文。下面介绍 Commitlint 和 Husky 配合,在提交 message 前自动使用 Commitlint 检查提交规范。

按照 Husky 入门教程 配置好 Husky, 使用下面的命令添加 Husky 的 commit-msg 的 hook:

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

配制好后,提交时,Commitlint 会自动检查 commit message 是否符合项目中 commitlint.config.js 文件配制的规范。

欢迎关注我的其它发布渠道