1

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

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

我收到一个提交错误,说:

⧗   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
4

2 回答 2

1

不幸的是,范围内不允许使用斜线。

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

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

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

[
  'ui-components',
  'ui-components--badge',
  'ui-components--button',
  'ui-components--tooltip',
   ...
]
于 2021-09-07T00:15:03.510 回答
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(
              ", "
            )}]`,
          ];
        },
      },
    },
  ],
}
于 2021-09-28T13:40:04.610 回答