【工具配置】快速配置commit规范(包括新版husky)

本文介绍如何利用Commitizen、commitlint和standard-version工具定制提交规范,以生成一致的changelog,提升团队协作效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  • 配置提交规范主要为了团队开发的提交格式统一,更好的生成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.

0.0.3 (2022-04-03)

0.0.2 (2022-04-03)

✨ Features | 新功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

业火之理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值