前言
- 配置提交规范主要为了团队开发的提交格式统一,更好的生成changelog。
所需库
Commitizen
- commitizen是为了便捷让我们通过选择来输入特定格式而不是通过自己输入来完成。
cz-customizable
- 可以在原生 Commitizen 的标准上,根据配置文件来自定义我们的提交规范。可以说是用来扩展 Commitizen 的神器。一般都用于 Commitizen 的配套使用。
commitlint
- commitlint 用来校验检查我们的提交 commit 是否符合设置的规范。
standard-version
- standard-version 是一款遵循语义化版本(semver)和 commit message 标准规范的版本自动化工具,它还可以使生成 changelog 自动化。
配置步骤
- 安装
pnpm install -D commitizen cz-customizable
- 添加pkg.json
"scripts": {
"commit": "git cz"
},
"config": {
"commitizen": {
"path": "cz-customizable"
}
}
- 新建.cz-config.js文件
module.exports = {
types: [
{ value: "feat", name: "feat 🍄: 新增新的特性" },
{ value: "fix", name: "fix 🐛: 修复 BUG" },
{ value: "docs", name: "docs 📄: 修改文档、注释" },
{
value: "refactor",
name: "refactor 🎸: 代码重构,注意和特性、修复区分开",
},
{ value: "perf", name: "perf ⚡: 提升性能" },
{ value: "test", name: "test 👀: 添加一个测试" },
{ value: "tool", name: "tool 🚗: 开发工具变动(构建、脚手架工具等)" },
{ value: "style", name: "style ✂: 对代码格式的修改不影响逻辑" },
{ value: "revert", name: "revert 🌝: 版本回滚" },
{ value: "update", name: "update ⬆: 第三方库升级 " },
],
scopes: [
{ name: "组件" },
{ name: "样式" },
{ name: "文档更改" },
{ name: "其它变更" },
],
allowTicketNumber: false,
isTicketNumberRequired: false,
ticketNumberPrefix: "TICKET-",
ticketNumberRegExp: "d{1,5}",
messages: {
type: "选择一种你的提交类型:",
scope: "选择一个scope (可选):",
customScope: "Denote the SCOPE of this change:",
subject: "简要说明:\n",
body: '详细说明,使用"|"换行(可选):\n',
breaking: "非兼容性说明 (可选):\n",
footer: "关联关闭的issue,例如:#31, #34(可选):\n",
confirmCommit: "确定提交?",
},
allowCustomScopes: true,
allowBreakingChanges: ["新增", "修复"],
subjectLimit: 100,
};
- 此时 使用npm run commit 可以生成提示提交。
- 下面安装:
pnpm install -D commitlint-config-cz @commitlint/cli husky
- commitlint-config.js
/*
* @Author: yehuozhili
* @Date: 2022-04-03 20:57:06
* @LastEditors: yehuozhili
* @LastEditTime: 2022-04-03 22:19:58
* @FilePath: \learn-stencil\commitlint.config.js
*/
module.exports = {
extends: ["cz"],
parserPreset: {
parserOpts: {
headerPattern: /^(\w*)\(([\u4e00-\u9fa5]*)\)/,
headerCorrespondence: ["type", "scope"],
},
},
rules: {
"type-empty": [2, "never"],
"scope-empty": [2, "never"],
},
};
- husky装完后使用
npx husky install
,此时会出现husky文件夹。 - 使用控制台执行以下命令:(注意!windows电脑使用git bash去执行该命令,否则报错)
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit $1'
- 生成一个commit-msg的文件,内容如下:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no -- commitlint --edit $1
- 因为某些原因生成不了也可以复制。
- 此时进行提交测试,发现会进入commitlint则成功。
- 下面安装:
pnpm install -D standard-version
- 增加scripts
"release": "standard-version"
- 记得要有个版本号,否则报错
- 增加.versionrc.js,其实就是changelog的映射
module.exports = {
types: [
{ 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 | 回退" },
{ type: "build", section: "📦 Build System | 打包构建" },
{ type: "update", section: "🚀 update | 构建/工程依赖/工具升级" },
{ type: "tool", section: "🚀 tool | 工具升级" },
{ type: "ci", section: "👷 Continuous Integration | CI 配置" },
],
};
- 使用命令,即可看见生成的changelog:
Changelog
All notable changes to this project will be documented in this file. See standard-version for commit guidelines.