From 42e21eb6025acd4ef76f0241cb7a4e6eb9b9261d Mon Sep 17 00:00:00 2001 From: pablone Date: Fri, 5 Apr 2024 14:13:17 +0200 Subject: [PATCH] feat(githook) add reference --- .husky/.commitlintrc.json | 6 ------ .husky/addReferenceTag.js | 36 ++++++++++++++++++++++++++++++++++++ .husky/commit-msg | 4 ++++ package.json | 3 ++- 4 files changed, 42 insertions(+), 7 deletions(-) delete mode 100644 .husky/.commitlintrc.json create mode 100644 .husky/addReferenceTag.js 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": [