Git 规范(Commitlint 可共享配置),用于对 git commit message 进行校验。
npm install @tembty/commitlint-config --save-dev
or
yarn add @tembty/commitlint-config --dev
// .commitlintrc.js
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const scopes = fs
.readdirSync(path.resolve(__dirname, 'src'), { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
// precomputed scope
const scopeComplete = execSync('git status --porcelain || true')
.toString()
.trim()
.split('\n')
.find((r) => r.indexOf('M ') !== -1)
?.replace(/(\/)/g, '%%')
?.match(/src%%((\w|-)*)/)?.[1];
module.exports = {
extends: ['@midea-infra'],
prompt: {
customScopesAlign: !scopeComplete ? 'top-bottom' : 'bottom',
defaultScope: scopeComplete,
scopes: [...scopes],
},
};
可通过 husky 设置在 git commit 时触发 commitlint。
首先安装 husky:
npm install husky --save-dev
or
yarn add husky --dev
然后在 package.json
中增加:
{
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
更多信息可参考 commitlint 文档。
预置 cz-git 引导式书写规范的 commit message,十分适合于不熟悉的同学。
在 package.json
中增加:
{
"config": {
"commitizen": {
"path": "./node_modules/cz-git"
}
}
}