diff --git a/.husky/.commitlintrc.json b/.husky/.commitlintrc.json deleted file mode 100644 index fae9c3c80..000000000 --- a/.husky/.commitlintrc.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "extends": ["@commitlint/config-conventional"], - "rules": { - "type-enum": [2, "always", ["ci", "chore", "docs", "ticket","feat", "fix", "perf", "refactor", "revert", "style", "test"]] - } -} \ No newline at end of file diff --git a/.husky/addReferenceTag.js b/.husky/addReferenceTag.js new file mode 100644 index 000000000..2eab2b8c5 --- /dev/null +++ b/.husky/addReferenceTag.js @@ -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); + } + } +} + diff --git a/.husky/commit-msg b/.husky/commit-msg index 5e563eba5..b813583c2 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,8 @@ #!/usr/bin/env sh . "$(dirname -- "$0")/_/husky.sh" +echo "Running husky commit-msg hook" npx --no-install commitlint --edit +echo "Adding reference tag to commit message" +pnpm run addReferenceTag + diff --git a/package.json b/package.json index 5aa1cfb7f..c6bafb510 100644 --- a/package.json +++ b/package.json @@ -115,7 +115,8 @@ "back": "nodemon --inspect -w modules ./node_modules/gulp/bin/gulp.js back", "lint": "eslint ./ --cache --ignore-pattern .gitignore", "commitlint": "commitlint --edit", - "prepare": "husky install" + "prepare": "husky install", + "addReferenceTag": "node .husky/addReferenceTag.js" }, "jest": { "projects": [