【问题标题】:Customise commitlint header format自定义 commitlint 标头格式
【发布时间】:2021-12-29 15:07:41
【问题描述】:

我正在使用 Husky 设置我的 git 挂钩,并尝试更改为 Commitlint 预期的默认标头格式:

type(scope?): subject

我特别想采用这种格式:

:gitmoji:? [scope] subject

:gitmoji:Gitmoji 的表情符号之一并且是可选的,在范围周围使用方括号(而不是可选)而不是括号,并且没有 : 将类型 + 范围与学科。此外,我希望 scope 具有类似于 TCKT-666 的格式(例如,引用 Jira 的票证),

目前,我一直在尝试使用来自commitlint.config.jsparserPresetparserOptsheaderPatternheaderCorrespondence 属性,但遇到了几个问题:

  • headerPattern 正则表达式似乎完全被忽略了,我得到的所有错误都来自我在commitlint.config.js 中设置的规则 - 所以我无法为我的scope 设置特定格式(尽管commitlint-plugin-function-rules 可能帮助)
  • 我完全不知道如何在类型之后删除对 : 的需要,或者如何用范围周围的方括号替换括号

【问题讨论】:

  • 您解决了这个问题吗?
  • 很遗憾,没有:/
  • 我有一个 POC,明天可能会用一个工作示例回答

标签: git githooks husky commitlint


【解决方案1】:

这应该适用于:gitmoji:? [scope] subject

module.exports = {
  parserPreset: {
    parserOpts: {
      headerPattern: /^(?:(:\w+:)\s)?\[(\w+)\] (.+)/,
      headerCorrespondence: ["type", "scope", "subject"],
    },
  },
  plugins: [
    {
      rules: {
        "header-match-team-pattern": (parsed) => {
          const { type, scope, subject } = parsed;
          if (type === null && scope === null && subject === null) {
            return [
              false,
              "header must be in format ':gitmoji:? [scope] subject'",
            ];
          }
          return [true, ""];
        },
        "gitmoji-type-enum": (parsed, _when, expectedValue) => {
          const { type } = parsed;
          if (type && !expectedValue.includes(type)) {
            return [
              false,
              `type must be one of ${expectedValue}
    see https://gitmoji.dev`,
            ];
          }
          return [true, ""];
        },
      },
    },
  ],
  rules: {
    // "type-empty": [2, "never"],
    "header-match-team-pattern": [2, "always"],
    "gitmoji-type-enum": [2, "always", [":bug:", ":sparkle:"]], // custom rule defined in plugins
    // "subject-case": [2, "always", "sentence-case"],
  },
};

看起来需要有一个像header-match-team-pattern 这样的自定义规则来确保 RegExp 匹配。

【讨论】:

  • 谢谢!我会在完成当前任务后立即尝试 ^^
  • 我一直无法使用explained-type-enum 规则,在commitlint.js.org/#/reference-rules 或npm 上也没有找到任何对应的规则或插件。有没有你用来完成这项工作的软件包?
  • 我假设自定义规则explained-type-enum 是应该在插件中定义的规则? (我在文档中没有找到与此相关的任何内容)我还尝试了一些东西,例如 git commit -m ":yahahalalala: [ Something else entirely"git commit -m "[ Something else entirely" 并且由于某种原因它们没有被 commitlint 阻止......
  • 我更新了答案,看看它是否适用于您的用例!
  • 现在完全可以用了,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2018-12-18
  • 2016-04-05
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多