Skip to content

Commit

Permalink
config: 🔧 add husky and commitlint
Browse files Browse the repository at this point in the history
Thank you to @Benjmain for his contribution 🙏
  • Loading branch information
Ravinou committed Jul 14, 2024
1 parent ee9a4f0 commit 3f57e31
Show file tree
Hide file tree
Showing 6 changed files with 1,198 additions and 15 deletions.
28 changes: 28 additions & 0 deletions .commitlintrc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'build',
'chore',
'config',
'doc',
'feat',
'fix',
'hotfix',
'i18n',
'refactor',
'revert',
'test',
'ui',
'wip',
],
],
},
ignores: [
(message) => message.includes('WIP'),
(message) => message.includes('wip'),
],
};
89 changes: 89 additions & 0 deletions .husky/append-icon.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

# define log prefix
prefix="pre-commit:"

# store message file, first and only param of hook
commitMessageFile="$1"

# breaking change icon !
boomIcon=':boom:'

# check for breaking change in file content
# find any line starting with 'BREAKING CHANGE'
function checkBreakingChangeInBody() {
breakingChange='BREAKING CHANGE'
while read -r line; do
if [[ "$line" == "$breakingChange"* ]]; then
echo "$prefix found $breakingChange in message body"
return 0
fi
done < "$1"
return 1
}

function findTypeIcon() {
# get message from 1st param
message="$1"

# declare an icons for each authorized enum-type from `.commitlintrc.js`
declare -A icons
icons[build]='🤖'
icons[chore]='🧹'
icons[config]='🔧'
icons[deploy]='🚀'
icons[doc]='📚'
icons[feat]=''
icons[fix]='🐛'
icons[hotfix]='🚑'
icons[i18n]='💬'
icons[publish]='📦'
icons[refactor]=''
icons[revert]=''
icons[test]=''
icons[ui]='🎨'
icons[wip]='🚧'
icons[WIP]='🚧'

for type in "${!icons[@]}"; do
# check if message subject contains breaking change pattern
if [[ "$message" =~ ^(.*)(!:){1}(.*)$ ]]; then
echo "$boomIcon"
return 0
# else find corresponding type icon
elif [[ "$message" == "$type"* ]]; then
echo "${icons[$type]}"
return 0
fi
done
return 1
}

# extract original message from the first line of file
message=$(head -n 1 <"$commitMessageFile")
echo "$prefix commit subject: '$message'"

if checkBreakingChangeInBody "$commitMessageFile"; then
echo 'setting breaking change icon'
icon=$boomIcon
else
icon=$(findTypeIcon "$message")
if [ $? -eq 1 ]; then
echo "$prefix ❌ unable to find icon corresponding to commit type. Make sure your commit-lint config (.commitlintrc.js) and append-msg script (append-msg.sh) types match"
exit 1
fi
fi

# check if icon has been appended before
if [[ "$message" == *"$icon"* ]]; then
echo "⏭️ skipping icon append as it's been added before"
exit 0
fi

# otherwise append icon
updatedMessage="${message/:/: $icon}"

# replace first line of file with updated message
sed -i "1s/.*/$updatedMessage/" "$commitMessageFile"

echo "$prefix ✅ appended icon $icon to commit message subject"
8 changes: 8 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
. "$(dirname "$0")/_/husky.sh"

# run commit lint
npx commitlint --edit "$1"

# run script to prepend message with icon
./.husky/append-icon.sh "$1"
8 changes: 8 additions & 0 deletions .husky/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
. "$(dirname "$0")/_/husky.sh"

# Check if it's an amend commit
if [ "$2" = "commit" ]; then
echo "Amendment detected, appending icon..."
./.husky/append-icon.sh "$1"
fi
Loading

0 comments on commit 3f57e31

Please sign in to comment.