Commitlint - 在范围枚举中允许“/”
Commitlint - Allow '/' in scope-enum
在我的 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
遗憾的是,范围中不允许使用斜杠。
为了解决这个问题,我将 /
替换为两个破折号 (--
)。
我写了一个脚本来抓取子文件夹和return一个数组:
https://gist.github.com/trevor-coleman/51f1730044e14081faaff098618aba36
[
'ui-components',
'ui-components--badge',
'ui-components--button',
'ui-components--tooltip',
...
]
根据源代码,Commitlint 将 /
用于多个范围。
这意味着,您可以像 fix(core/account): fix border
一样提交,但不能提交 fix(ui-components/tooltip): fix border
,因为您需要先将 tooltip
添加到您的范围中。
您可以编写自己的自定义插件来检查范围,我遇到了同样的问题,所以我写了一个来解决这个问题,请参见下面的示例 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(
", "
)}]`,
];
},
},
},
],
}
在我的 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
遗憾的是,范围中不允许使用斜杠。
为了解决这个问题,我将 /
替换为两个破折号 (--
)。
我写了一个脚本来抓取子文件夹和return一个数组:
https://gist.github.com/trevor-coleman/51f1730044e14081faaff098618aba36
[
'ui-components',
'ui-components--badge',
'ui-components--button',
'ui-components--tooltip',
...
]
根据源代码,Commitlint 将 /
用于多个范围。
这意味着,您可以像 fix(core/account): fix border
一样提交,但不能提交 fix(ui-components/tooltip): fix border
,因为您需要先将 tooltip
添加到您的范围中。
您可以编写自己的自定义插件来检查范围,我遇到了同样的问题,所以我写了一个来解决这个问题,请参见下面的示例 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(
", "
)}]`,
];
},
},
},
],
}