【问题标题】:Commitlint - Allow '/' in scope-enumCommitlint - 在范围枚举中允许“/”
【发布时间】:2021-08-30 23:54:11
【问题描述】:

在我的 Angular 项目中,我想用一些预定义的 scopes 扩展 @commitlint/config-conventional。

Angular 项目有一个用于 UI 组件的库(通过 ng generate library 生成)和一个使用 UI 库的默认应用。

在commitlint.config.js 我添加了以下几行:

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    'scope-enum': [
        2,
        'always',
        [
          'ui-components',
          'ui-components/badge',
          'ui-components/button',
          'ui-components/tooltip',
          'core',
          'account',
          'plugins',
          'settings',
          'projects',
          'shared',
          'styles'
        ]
    ]
  }
};

但是,当我尝试在范围内提交某些内容时:'ui-components/tooltip':

fix(ui-components/tooltip): fix border

我收到一个 commitlint 错误,说:

⧗   input: fix(ui-components/tooltip): fix border
✖   scope must be one of [ui-components, ui-components/badge, ui/button, ui-components/tooltip, core, account, plugins, settings, projects, shared, styles] [scope-enum]

✖   found 1 problems, 0 warnings

【问题讨论】:

    标签: conventional-commits conventional-changelog


    【解决方案1】:

    根据源码,Commitlint 对多个作用域使用/。

    这意味着,您可以像 fix(core/account): fix border 一样提交,但您不能提交 fix(ui-components/tooltip): fix border,因为您需要先将 tooltip 添加到您的作用域中。

    这里是源代码:https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/rules/src/scope-enum.ts

    另外,这里也提到了:https://github.com/conventional-changelog/commitlint/blob/master/docs/concepts-commit-conventions.md#multiple-scopes

    您可以编写自己的自定义插件来检查范围,我遇到了同样的问题,所以我写了一个来解决这个问题,请参见下面的示例commitlint.config.js:

    module.exports = {
      extends: ["@commitlint/config-conventional"],
      rules: {
        "enhanced-scope-enum": [
          2,
          "always",
          [
            "ui-components",
            "ui-components/badge",
            "ui-components/button",
            "ui-components/tooltip",
            "core",
            "account",
            "plugins",
            "settings",
            "projects",
            "shared",
            "styles",
          ],
        ],
      },
      plugins: [
        {
          rules: {
            "enhanced-scope-enum": (parsed, when = "always", value = []) => {
              if (!parsed.scope) {
                return [true, ""];
              }
    
              // only use comma sign as seperator
              const scopeSegments = parsed.scope.split(",");
    
              const check = (value, enums) => {
                if (value === undefined) {
                  return false;
                }
                if (!Array.isArray(enums)) {
                  return false;
                }
                return enums.indexOf(value) > -1;
              };
    
              const negated = when === "never";
              const result =
                value.length === 0 ||
                scopeSegments.every((scope) => check(scope, value));
    
              return [
                negated ? !result : result,
                `scope must ${negated ? `not` : null} be one of [${value.join(
                  ", "
                )}]`,
              ];
            },
          },
        },
      ],
    }
    

    【讨论】:

      【解决方案2】:

      很遗憾,范围内不允许使用斜线。

      为了解决这个问题,我将 / 替换为两个破折号 (--)。

      我写了一个脚本来抓取子文件夹并返回一个数组:

      https://gist.github.com/trevor-coleman/51f1730044e14081faaff098618aba36

      [
        'ui-components',
        'ui-components--badge',
        'ui-components--button',
        'ui-components--tooltip',
         ...
      ]
      

      【讨论】:

        猜你喜欢
        • 2012-01-19
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多