Skip to content

Commit

Permalink
fix(commitlint): 🐛 fix emoji commit
Browse files Browse the repository at this point in the history
  • Loading branch information
realashleybailey committed Nov 8, 2023
1 parent 56d7b47 commit 4155faf
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 15 deletions.
19 changes: 4 additions & 15 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,8 @@ npx --no-install commitlint --edit "$1"
filepath="$1"
tmp_file=$(mktemp /tmp/emoji-commitzen.XXXX)

replacements=(
's/^feat/✨ feat/'
's/^fix/🐝 fix/'
's/^doc/📚 doc/'
's/^style/🎨 style/'
's/^refactor/🔨 refactor/'
's/^perf/🚀 perf/'
's/^chore/🔧 chore/'
's/^lint/💄 lint/'
's/^test/🚨 test/'
's/^first/🐣 first/'
)
while IFS= read -r line; do
echo "$line" | node "$(dirname -- "$0")/emojify-commit-message.js" >> "$tmp_file"
done < "$filepath"

sed_command=$(printf "%s;" "${replacements[@]}")
cat $filepath | sed "$sed_command" > $tmp_file
mv $tmp_file $filepath
mv "$tmp_file" "$filepath"
72 changes: 72 additions & 0 deletions .husky/emojify-commit-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const regex = [
{
regex: /(?<=^feat(\(.*\))?(:)?)\s/,
emoji: ' ✨ '
},
{
regex: /(?<=^fix(\(.*\))?(:)?)\s/,
emoji: ' 🐛 '
},
{
regex: /(?<=^docs(\(.*\))?(:)?)\s/,
emoji: ' 📚 '
},
{
regex: /(?<=^style(\(.*\))?(:)?)\s/,
emoji: ' 💎 '
},
{
regex: /(?<=^refactor(\(.*\))?(:)?)\s/,
emoji: ' 📦 '
},
{
regex: /(?<=^perf(\(.*\))?(:)?)\s/,
emoji: ' 🚀 '
},
{
regex: /(?<=^test(\(.*\))?(:)?)\s/,
emoji: ' 🚨 '
},
{
regex: /(?<=^build(\(.*\))?(:)?)\s/,
emoji: ' 👷 '
},
{
regex: /(?<=^ci(\(.*\))?(:)?)\s/,
emoji: ' 💚 '
},
{
regex: /(?<=^chore(\(.*\))?(:)?)\s/,
emoji: ' 🔧 '
},
{
regex: /(?<=^revert(\(.*\))?(:)?)\s/,
emoji: ' ⏪ '
},
{
regex: /(?<=^release(\(.*\))?(:)?)\s/,
emoji: ' 🏹 '
},
{
regex: /(?<=^dependabot(\(.*\))?(:)?)\s/,
emoji: ' 📦 '
},
{
regex: /(?<=^first(\(.*\))?(:)?)\s/,
emoji: ' 🎉 '
}
]

// Emojify a commit message
function emojifyCommitMessage(commitMessage) {
return regex.reduce((acc, { regex, emoji }) => {
return acc.replace(regex, emoji)
}, commitMessage)
}

// Allow this to be run as a script from the command line
if (require.main === module) {
const args = process.argv.slice(2)
const commitMessage = args[0]
console.log(emojifyCommitMessage(commitMessage))
}
89 changes: 89 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

regex_emojis=(
"(?<=^feat(\(.*\))?(:)?)\s|✨"
"(?<=^fix(\(.*\))?(:)?)\s|🐛"
"(?<=^docs(\(.*\))?(:)?)\s|📚"
"(?<=^style(\(.*\))?(:)?)\s|💎"
"(?<=^refactor(\(.*\))?(:)?)\s|📦"
"(?<=^perf(\(.*\))?(:)?)\s|🚀"
"(?<=^test(\(.*\))?(:)?)\s|🚨"
"(?<=^build(\(.*\))?(:)?)\s|👷"
"(?<=^ci(\(.*\))?(:)?)\s|💚"
"(?<=^chore(\(.*\))(:)?)\s|🔧"
"(?<=^revert(\(.*\))(:)?)\s|⏪"
"(?<=^release(\(.*\))(:)?)\s|🏹"
"(?<=^dependabot(\(.*\))(:)?)\s|📦"
"(?<=^first(\(.*\))(:)?)\s|🎉"
)

tests=(
"feat test"
"feat: test"
"feat(test) test"
"feat(test): test"
"fix test"
"fix: test"
"fix(test) test"
"fix(test): test"
"docs test"
"docs: test"
"docs(test) test"
"docs(test): test"
"style test"
"style: test"
"style(test) test"
"style(test): test"
"refactor test"
"refactor: test"
"refactor(test) test"
"refactor(test): test"
"perf test"
"perf: test"
"perf(test) test"
"perf(test): test"
"test test"
"test: test"
"test(test) test"
"test(test): test"
"build test"
"build: test"
"build(test) test"
"build(test): test"
"ci test"
"ci: test"
"ci(test) test"
"ci(test): test"
"chore test"
"chore: test"
"chore(test) test"
"chore(test): test"
"revert test"
"revert: test"
"revert(test) test"
"revert(test): test"
"release test"
"release: test"
"release(test) test"
"release(test): test"
"dependabot test"
"dependabot: test"
"dependabot(test) test"
"dependabot(test): test"
"first test"
"first: test"
"first(test) test"
"first(test): test"
)

for test in "${tests[@]}"; do
for regex_emoji in "${regex_emojis[@]}"; do
regex="${regex_emoji%%|*}"
emoji="${regex_emoji##*|}"

if [[ "$test" =~ $regex ]]; then
test="${test/$regex/$emoji}"
echo "$test"
fi
done
done

0 comments on commit 4155faf

Please sign in to comment.