feat(githook) add reference

This commit is contained in:
Pablo Natek 2024-04-05 14:13:17 +02:00
parent 0004d1da23
commit 42e21eb602
4 changed files with 42 additions and 7 deletions

View File

@ -1,6 +0,0 @@
{
"extends": ["@commitlint/config-conventional"],
"rules": {
"type-enum": [2, "always", ["ci", "chore", "docs", "ticket","feat", "fix", "perf", "refactor", "revert", "style", "test"]]
}
}

36
.husky/addReferenceTag.js Normal file
View File

@ -0,0 +1,36 @@
const fs = require('fs');
const path = require('path');
async function getCurrentBranchName(p = process.cwd()) {
while (p !== path.parse(p).root) {
const gitHeadPath = path.join(p, '.git', 'HEAD');
try {
const headContent = await fs.readFile(gitHeadPath, 'utf-8');
return headContent.trim().split('/')[2];
} catch (err) {
p = path.resolve(p, '..');
}
}
return false;
}
const branchName = getCurrentBranchName();
if (branchName) {
const msgPath = `.git/COMMIT_EDITMSG`;
const msg = fs.readFileSync(msgPath, "utf-8");
const referenceTag = `refs #${branchName.match(/^\d+/)}`;
console.log('referenceTag: ', referenceTag);
console.log(msg);
if (!msg.includes(referenceTag)) {
const splitedMsg = msg.split(':');
if (splitedMsg.length > 1) {
const finalMsg = splitedMsg[0] + splitedMsg.slice(1).join(':');
fs.writeFileSync(msgPath, finalMsg);
}
}
}

View File

@ -1,4 +1,8 @@
#!/usr/bin/env sh #!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh" . "$(dirname -- "$0")/_/husky.sh"
echo "Running husky commit-msg hook"
npx --no-install commitlint --edit npx --no-install commitlint --edit
echo "Adding reference tag to commit message"
pnpm run addReferenceTag

View File

@ -115,7 +115,8 @@
"back": "nodemon --inspect -w modules ./node_modules/gulp/bin/gulp.js back", "back": "nodemon --inspect -w modules ./node_modules/gulp/bin/gulp.js back",
"lint": "eslint ./ --cache --ignore-pattern .gitignore", "lint": "eslint ./ --cache --ignore-pattern .gitignore",
"commitlint": "commitlint --edit", "commitlint": "commitlint --edit",
"prepare": "husky install" "prepare": "husky install",
"addReferenceTag": "node .husky/addReferenceTag.js"
}, },
"jest": { "jest": {
"projects": [ "projects": [