// commitlint.config.ts
import { execSync } from 'node:child_process'
const scopes = ['views', 'components', 'router', 'store', 'utils', 'styles', 'assets', 'types', 'other']
const gitStatus = execSync('git status --porcelain || true').toString().trim().split('\n')
const scopeComplete = gitStatus.find(r => ~r.indexOf('M src'))?.replace(/(\/)/g, '%%')?.match(/src%%((\w|-)*)/)?.[1];
const subjectComplete = gitStatus.find(r => ~r.indexOf('M src'))?.replace(/\//g, '%%')?.match(/src%%((\w|-)*)/)?.[1];
export default {
rules: {
'scope-enum': [2, 'always', scopes],
'body-leading-blank': [1, 'always'],
'footer-leading-blank': [1, 'always'],
'header-max-length': [2, 'always', 72],
'scope-case': [2, 'always', 'lower-case'],
'subject-case': [1, 'never', ['sentence-case', 'start-case', 'pascal-case', 'upper-case']],
'subject-empty': [2, 'never'],
'subject-full-stop': [2, 'never', '.'],
'type-case': [2, 'always', 'lower-case'],
'type-empty': [2, 'never'],
'type-enum': [2, 'always', ['build', 'chore', 'ci', 'docs', 'feat', 'fix', 'perf', 'refactor', 'revert', 'release', 'style', 'test', 'improvement']],
},
prompt: {
defaultScope: scopeComplete,
customScopesAlign: !scopeComplete ? 'top' : 'bottom',
defaultSubject: subjectComplete && `[${subjectComplete}] `,
allowCustomIssuePrefixs: false,
allowEmptyIssuePrefixs: false,
},
}
// package.json
{
"scripts": {
"cz": "czg"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-git"
}
},
"lint-staged": {
"*.{vue,js,ts,jsx,tsx,md,json}": "eslint --fix"
}
}
# Initialize husky
npx husky-init
# Add commit-msg hook
npx husky add .husky/commit-msg 'pnpm exec commitlint --config commitlint.config.cjs --edit "${1}"'