docs(message): add

This commit is contained in:
zjkjzj
2021-03-18 15:25:47 +08:00
parent 272f8b62cd
commit f573027057
8 changed files with 548 additions and 10 deletions

View File

@@ -0,0 +1,125 @@
# [CommitLint][Husky]消息校验工具
参考:[commitlint](https://conventional-changelog.github.io/commitlint/#/)
使用`commitizen`可以规范化提交信息,同样的,可以设置工具来检查提交信息是否符合格式要求
* [commitlint](https://github.com/conventional-changelog/commitlint):用于检查提交信息
* [husky](https://github.com/typicode/husky)是`hook`工具,作用于`git-commit``git-push`阶段
使用场景:
1. 本地信息检查
2. `CI`信息检查
## `commitlint`
### 全局安装
$ npm install -g @commitlint/cli @commitlint/config-conventional
### 本地安装
$ npm install --save-dev @commitlint/{cli,config-conventional}
### 配置规范
# 使用conventional规范
$ echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
### 测试
需要全局安装才能在命令行使用`commitlint`
# 错误示例,类型后跟一个冒号和一个空格
$ echo "docs:添加新的文档" | commitlint
⧗ input: docs:添加新的文档
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
(Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
# 正确示例
$ echo "docs: 添加新的文档" | commitlint
⧗ input: docs: 添加新的文档
✔ found 0 problems, 0 warnings
(Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
# 或者
$ commitlint "docs: asdf"
⧗ input: build(npm): 添加package.json
✔ found 0 problems, 0 warnings
(Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
## `husky`
### 安装
# 本地
$ npm install --save-dev husky
### 配置
`package.json`中添加
// package.json
{
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
`git`提交信息提交`commitlint`检查,参数`-E`默认将`.git/COMMIT_EDITMSG`数据添加到`HUSKY_GIT_PARAMS`
### 测试
# 错误提交提交
$ git commit -m "忽略node_modules"
husky > commit-msg (node v10.15.3)
⧗ input: 忽略node_modules
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
(Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
husky > commit-msg hook failed (add --no-verify to bypass)
# 正确提交
$ git commit -m "build(npm): 忽略node_modules"
husky > commit-msg (node v10.15.3)
⧗ input: build(npm): 忽略node_modules
✔ found 0 problems, 0 warnings
(Need help? -> https://github.com/conventional-changelog/commitlint#what-is-commitlint )
[master 8728336] build(npm): 忽略node_modules
1 file changed, 1 insertion(+)
create mode 100644 .gitignore
### 禁用`husky`
某一次提交想要禁用`husky`,可以添加参数`--no-verify`
$ git commit --no-verify -m "xxx"
## `CI`设置
`Travis CI`为例
在本地安装
$ npm install --save-dev @commitlint/travis-cli
修改`travis.yml`
language: node_js
script:
- commitlint-travis

View File

@@ -0,0 +1,85 @@
# [Commitizen]信息交互工具
[Commitizen](http://commitizen.github.io/cz-cli/)是一个提交日志工具,辅助开发者使用提交规则
## 预配置
需要安装`NodeJS`,参考:[nodeJS安装](https://hexo-guide.readthedocs.io/zh_CN/latest/node/nodeJS%E5%AE%89%E8%A3%85.html)
## 安装`Commitizen`
# 全局安装
$ npm install -g commitizen
## 安装`Adapter`
`Commitizen`支持多种不同的提交规范,可以安装和配置不同的适配器实现
`Conventional Commit`规范为例
### 全局配置(推荐)
# 安装
$ npm install -g cz-conventional-changelog
# 配置
$ echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
### 本地配置
# 安装
commitizen init cz-conventional-changelog --save-dev --save-exact
安装完成后,查看是否在`package.json`中已加入`cz-conventional-changelog`信息:
```
...
...
"devDependencies": {
"cz-conventional-changelog": "^3.0.2"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
```
### 测试
安装完成后,使用`git-cz`替代`git commit`完成提交操作,
```
$ git cz
cz-cli@4.0.3, cz-conventional-changelog@3.0.2
? Select the type of change that you're committing: (Use arrow keys)
feat: A new feature
fix: A bug fix
docs: Documentation only changes
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
refactor: A code change that neither fixes a bug nor adds a feature
perf: A code change that improves performance
test: Adding missing tests or correcting existing tests
(Move up and down to reveal more choices)
```
通过提示信息辅助你完成标准化的提交日志
**`git-cz`支持`git commit`所有的参数设置**
**正文换行使用`\n`操作,比如正文内容如下:`新增事项:\n1. 测试1\n2. 测试2\n3. 测试`**
build(npm): 加入package.json
新增事项:
1. 测试1
2. 测试2
3. 测试
## 徽章
`README`中添加`commitizen`友好徽章
[![](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)