cz-cubics
一个可定制的 Commitizen 带有emoji前缀的常规提交消息的适配器,加上一个准备扩展 commitlint 配置。
安装
pnpm add -D cz-cubics commitizen
将其注册为commizizen适配器 package.json:
{
"config": {
"commitizen": {
"path": "./node_modules/cz-cubics"
}
}
}
或在 .czrc:
{
"path": "./node_modules/cz-cubics"
}
然后承诺:
pnpm commit
(假设一个 "commit": "cz" 脚本,或运行 commitizen/git cz 直接)
配置
Config从下面最近的文件中解析,通过 find-up 顺序如下:
.czrcpackage.json
的第一个匹配文件 config["cz-cubics"] 如果两者都存在,则关键获胜; .czrc 优先。
配置文件形状
.czrc
{
"config": {
"cz-cubics": {
"headFormat": "{type}{scope}: {subject}"
}
}
}
package.json
{
"config": {
"commitizen": {
"path": "node_modules/cz-cubics"
},
"cz-cubics": {
"headFormat": "{type}{scope}: {subject}"
}
}
}
请注意 cz-cubics 坐着就像 兄弟姐妹 的 commitizen 下 config,而不是嵌套在其中。
自定义配置将被验证,无效类型,未知 skipQuestions 值,或未识别 {token}年代 headFormat 抛出错误,而不是静默失败或在第一个问题上失败。
CZCubicsConfig
| 财产 | 类型 | 默认的 | 描述 |
|---|---|---|---|
types | CZCubicsCommitType[] | 的内容 defaultTypes.json | 提供给用户的提交类型列表。 |
skipQuestions | string[] | [] | 跳过以下任何一项: scope, body, issues, breaking。 type 和 subject 是必须的,不能跳过。 |
scopes | string[] | - | 在作用域自动完成提示符中显示预定义的作用域选择(query .js的选择数组)。 |
questions | Object | - | 覆盖生成的查询器问题。 |
subjectMaxLength | number | 100 | 主题行的最大允许长度。 |
headFormat | string | "{emoji} {type}{scope}: {subject}" | 提交头的模板。支持以下令牌。 |
CZCubicsCommitType
每一项 types (在 defaultTypes.json)的形状为:
{
"name": "feat",
"emoji": "✨",
"description": "A new feature"
}
| 财产 | 类型 | 默认的 | 描述 |
|---|---|---|---|
name | string | - | 常规的提交类型关键字(例如: feat, fix). |
emoji | string | "" | 表情符号显示在文字旁边,并通过 {emoji}。 |
description | string | - | 类型选择提示中显示的解释性文本。 |
headFormat 令牌(CZCubicsHeaderTokens)
| 令牌 | 解析为 |
|---|---|
{emoji} | 所选类型 emoji。 |
{type} | 所选类型 name。 |
{scope} | (scope) 如果输入了作用域,则为空。 |
{subject} | 修剪过的主题行。 |
任何由省略的记号(例如,没有作用域)留下的连续空白将被折叠为单个空格。
例子 .czrc
{
"config": {
"cz-cubics": {
"types": [
{ "name": "feat", "emoji": "✨", "description": "A new feature" },
{ "name": "fix", "emoji": "🐛", "description": "A bug fix" }
],
"scopes": ["api", "ui", "docs"],
"skipQuestions": ["issues", "breaking"],
"subjectMaxLength": 72,
"headFormat": "{emoji} {type}{scope}: {subject}"
}
}
}
提交消息结构
最后的信息集合如下:
<head>
<body>
BREAKING CHANGE: <breakingBody>
<footer (issues)>
- 头 =>建立从
headFormat,截断为终端的列宽度。 - 身体 =>自由文本正文答案,绕到终端宽度。可以使用管道添加换行符
| - 打破 =>仅在提供破坏更改主体时包含。可以使用管道添加换行符
| - 页脚 =>问题引用,格式化通过
formatIssues。
空的部分被省略;结果是清除尾随空格。
commitlint 集成
这个包自带 commitlint.config.js,消费者可以直接扩展这些规则,而不是复制规则:
/** @type {import("@commitlint/types").UserConfig} */
module.exports = {
extends: ["./node_modules/cz-cubics/commitlint.config.js"],
// add or override rules here
};
当 type 在 package.json 是 "module" 也可以使用CJS版本
/** @type {import("@commitlint/types").UserConfig} */
module.exports = {
extends: ["./node_modules/cz-cubics/commitlint.config.cjs"],
// add or override rules here
};
它包括:
type-empty:从来没有,subject-empty:从来没有,subject-case:sentence-case,总是强制执行。subject-max-length:100,总是强迫,从你的决心subjectMaxLength配置。type-enum:仅限于name的每一个条目types配置,因此只有您配置的提交类型通过。header-start-emoji:总是强制头必须以一个开头 Extended_Pictographic 角色(表情符号),与主角匹配{emoji}令牌的headFormat。header-has-emoji:默认关闭0)相同的表情符号检测,但检查整个标题行,而不仅仅是前导符号。启用它(严重性)2),"always"如果你想让表情符号在标题的任何地方被允许,而不是只在开头。- 一个
parserPreset谁的headerPattern和headerCorrespondence是 动态构建从您的解析headFormat,而不是硬编码。这意味着:- 改变
headFormat在你的配置中自动更改过滤器解析和验证的内容。 - 所需令牌(
{type},{subject})使用匹配策略,即使其中一个是空的,仍然可以让整个头解析成功 - 可选令牌(
{emoji},{scope})在省略时被正确地视为缺席,而不需要占位符值。
- 改变
需求
cz-cubics/commitlint.config.js 是一个普通的配置对象,运行时不依赖于任何提交包本身。然而,要真正检测提交,你需要 @commitlint/cli 安装 在你自己的项目中,因为这是读取和执行配置:
pnpm add -D @commitlint/cli
沙哑的钩
把它连接起来 .husky/commit-msg:
pnpm commitlint --edit "$1"
确保钩子文件是可执行的(chmod +x .husky/commit-msg)和 core.hooksPath 点 .husky (由自动设置 prepare: "husky" 脚本)。
许可证
麻省理工学院
特别感谢
- ngryman / cz-emoji:基于表情符号的常规提交类型的核心思想是这个适配器的基础。
- commitizen / cz-cli:此包插入的适配器框架。