CZ工具集合
编写于<2022-03-24> 使用工具集进行代码规范 + 提交规范的校验
commitizen (^4.2.4)
交互式提交,规范可参考上方连接的约定式提交
配置可添加commitlint.config.js
npm i commitizen -D # git commit 交互工具
npx commitizen init cz-conventional-changelog --save-dev --save-exact
// package.json
{
// 自动生成的规则包配置
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
然后可以执行后会添加下方的东西
"scripts": {
"commit" : "cz",
// 代替 git commit, 交互式填写 commit 信息
"release": "standard-version -t $(date +release-%Y%m%d-v)",
// 该命令做了什么: 更新版本号 >> 更新CHANGELOG.md >> git 打标签 >> git add . >> git commit
// 标签名示例: release-20211223-v1.0.1
"release:first": "standard-version --first-release -t $(date +release-%Y%m%d-v)",
// 第一次发版用: 不会更新版本号
}
// husky 校验的配置 commitlint.config.js
module.exports = {
extends: ["@commitlint/config-conventional"],
// 以下时我们自定义的规则
rules: {
"type-enum": [
2,
"always",
[
"bug", // 此项特别针对bug号,用于向测试反馈bug列表的bug修改情况
"feat", // 新功能(feature)
"fix", // 修补bug
"init",
"docs", // 文档(documentation)
"style", // 格式(不影响代码运行的变动)
"refactor", // 重构(即不是新增功能,也不是修改bug的代码变动)
"test", // 增加测试
"build", // 构建
"chore", // 构建过程或辅助工具的变动
"perf", // 优化相关,比如提升性能、体验
"revert", // feat(pencil): add ‘graphiteWidth’ option (撤销之前的commit)
"merge", // 合并分支, 例如: merge(前端页面): feature-xxxx修改线程地址
],
],
},
};
对于哪些习惯使用命令行进行提交代码的,或者IDE/编辑器进行的提交进行git hook进行强校验,工具为husky
husky(^7.0.0)
# 如果 采用angular规范 @commitlint/config-conventional和@commitlint/config-angular 应该可替换
pnpm add husky @commitlint/config-angular @commitlint/cli -D
在"script"
添加
"prepare": "husky install"
如果执行过pnpm install
后加装的husky
,请手动执行下pnpm prepare
.husky/commit-msg
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no-install commitlint --edit $1
.husky/pre-commit
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
# 这里应该执行代码规范校验,此处不再校验代码规范,因为没有规范
echo "请规范提交版本提交注释以及内容"
注意: 这里的初始化,可以使用npx husky-init
pnpm/yarn dlx husky-init
,如果出现一下错误,可以尝试执行chmod +x .husky/pre-commit
和 hmod +x .husky/commit-msg
hint: The '.husky/pre-commit' hook was ignored because it’s not set as executable.
hint: You can disable this warning with git config advice.ignoredHook false.
hint: The '.husky/commit-msg' hook was ignored because it’s not set as executable.
hint: You can disable this warning with git config advice.ignoredHook false.
如果有提交完进行版本修复或者其他提交升级版本并且记录版本迭代 可采用 standard-version
standard-version(^9.3.2)
pnpm --save-dev standard-version cz-conventional-changelog
版本的配置可以放在 .versionrc
, .versionrc.json
or .versionrc.js
// .versionrc.js
module.exports = {
types: [
{ type: "bug", section: "🐛 Special bug | 特定bug修复" },
{ type: "feat", section: "✨ Features | 新功能" },
{ type: "fix", section: "🐛 Bug Fixes | Bug 修复" },
{ type: "init", section: "🎉 Init | 初始化" },
{ type: "docs", section: "✏️ Documentation | 文档" },
{ type: "style", section: "💄 Styles | 风格" },
{ type: "refactor", section: "♻️ Code Refactoring | 代码重构" },
{ type: "perf", section: "⚡ Performance Improvements | 性能优化" },
{ type: "test", section: "✅ Tests | 测试" },
{ type: "revert", section: "⏪ Revert | 回退", hidden: true },
{ type: "build", section: "📦 Build System | 打包构建" },
{ type: "chore", section: "🚀 Chore | 构建/工程依赖/工具" },
{ type: "ci", section: "👷 Continuous Integration | CI 配置" },
{ type: "merge", section: "🧬 Merge | 合并分支" },
],
};