const { execSync } = require('child_process');
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'feat',
'fix',
'docs',
'style',
'refactor',
'perf',
'test',
'chore',
'revert',
'build',
'ci'
]
],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'header-max-length': [2, 'always', 100],
'ci-files-required': [2, 'always']
},
plugins: [
{
rules: {
'ci-files-required': ({type}) => {
if (type !== 'ci') {
return [true];
}
try {
const stagedFiles = execSync('git diff --cached --name-only', { encoding: 'utf-8' })
.trim()
.split('\n')
.filter(Boolean);
const hasCIFiles = stagedFiles.some(file =>
file.includes('.github/workflows/') ||
file.includes('.github/actions/') ||
file.includes('jest.config.js') ||
file.includes('commitlint.config.js') ||
file.includes('.eslintrc') ||
file.includes('.prettierrc') ||
(file === 'package.json')
);
if (!hasCIFiles) {
return [false, 'ci类型的提交必须包含CI相关文件的修改(.github/workflows/, 测试配置, lint配置等)'];
}
return [true];
} catch (error) {
console.warn('Warning: Could not check staged files:', error.message);
return [true];
}
}
}
}
]
};