Skip to main content

cz-customizable

一个可以自定义git commit时格式的常用工具

常用.cz-config.js文件

module.exports = {

  types: [
    { value: "✨feat", name: "新增:    新增功能或者特性" },
    { value: "⚡️perf", name: "优化:    优化相关,比如提升性能、体验" },
    { value: "💄style", name: "格式:    💄仅仅修改了空格、格式缩进等等,不改变代码逻辑" },
    { value: "✅test", name: "测试:    测试用例,包括增加缺失用例或者修正测试用例" },
    { value: "🐛fix", name: "修复:    修复Bug" },
    { value: "📝docs", name: "文档:    仅仅修改了文档,比如`README`, `CHANGELOG`, CONTRIBUTE等等" },
    { value: "♻️refactor", name: "重构:    重构(即不是新增功能,也不是修改bug的代码变动)" },
    { value: "🔧chore", name: "工具:    构建过程或辅助工具的变动" },
    { value: "⏪revert", name: "回滚:    用于撤销以前的 commit,后面跟着被撤销 Commit 的 Header。" },
    { value: "🔃ci", name: "更新:    更改我们的CI配置文件和脚本" },
  ],

  scopes: [{ name: "custom" }, { name: "leetcode" }, { name: "javascript" }, { name: "typescript" }, { name: "Vue" }, { name: "node" }],

  // it needs to match the value for field type. Eg.: 'fix'
  scopeOverrides: {
    fix: [{ name: "merge" }, { name: "style" }, { name: "e2eTest" }, { name: "unitTest" }],
  },

  // override the messages, defaults are as follows
  messages: {
    type: "选择一种你的提交类型:",
    scope: "选择一个scope (可选):",

    // used if allowCustomScopes is true
    // customScope: "Denote the SCOPE of this change:",
    customScope: "选择一个scope (可选):",
    subject: "短说明:\n",
    body: '长说明,使用"|"换行(可选):\n',
    breaking: "非兼容性说明 (可选):\n",
    footer: "关联关闭的issue,例如:#31, #34(可选):\n",
    confirmCommit: "确定提交说明?(yes/no)",
  },

  allowCustomScopes: true,
  allowBreakingChanges: ["特性", "修复"],

  // limit subject length
  subjectLimit: 100,
};

BUG

无法使用自定义输入Scope

引用自:https://github.com/leoforfree/cz-customizable/issues/215

现有情况说明

cz-customizable 有2种使用模式。

  1. standalone mode
  2. cz-customizable as commitizen plugin

我查阅了之前关于这个问题的相关讨论, 最后 issues201 的解决方案是合并了 pull202

这个解决方案是对项目使用的 inquirer 进行了版本降级处理

这个处理方案只对 standalone mode 有用, 因为 standalone mode 会使用项目内的 inquirer 库进行提问

但是当使用 cz-customizable as commitizen plugin mode 模式时, 提问时使用的库是 commitizen 自带的 inquirer 库

commitizen@4.2.5 更新了自带的 inquirer 库到 v8

所以当使用 commitizen 的版本大于 4.2.4 的时候, 这个问题任然存在, 没有被修复

当你运行 cz 指令时, 你可以看到现在是基于什么模式在运行, 在你的截图可以看到是 cz-cli@4.2.6 + cz-customizable@7.0.0 的组合, 这个情况下该问题未被修复

当前问题最直接的解决方案

因为是 commitizen 的 inquirer 进行了版本更新导致的, 所以最简单的解决方案就是进行版本降级, 安装最后能支持的版本 commitizen@4.2.4

问题原因

查看代码以及相关讨论, 可知不会询问是因为自定义范围范围选择使用了相同的问题 name, 新版本的 inquirer 有个特性是会自动跳过已经有答案的问题

修复这个问题最简单的处理方案为 pull214, 即增加 askAnswered: true 参数, 但是该方案并未被正式合并

我猜测原因是因为之前已经合并了 pull202, 导致 inquirer 库版本已经退回, 无法使用该参数

另外 pull203 的修改也解决了该问题, 因为使用了不同的 name 来询问, 但是该方案也未被正式合并, 不过作者额外创建了 cz-custom 库, 解决了该问题

解决方案

  1. 安装 commitizen@4.2.4 版本
  2. 使用 cz-custom 库代替 cz-customizable, 当然后续更新能不能跟上是个问题, 一般能不换肯定不换
yarn remove cz-customizable
yarn add -D cz-custom

// package.json 中config修改如下
"config": {
"commitizen": {
-- "path":"node_modules/cz-customizable"
++ "path": "node_modules/cz-custom",
}
}
  1. 通过 patch-package 按照 issues212 的方案, 只需增加一行配置项 askAnswered: true, 即可解决问题