Last active
December 23, 2024 14:58
-
-
Save hrasekj/ec6eb27491643306aed3e04efbd1fda6 to your computer and use it in GitHub Desktop.
Husky pre-commit hook, that will lint or check only staged changes using biomejs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
# get staged file names | |
unstaged_files=$(git diff --name-only) | |
staged_files=$(git diff --cached --name-only) | |
err_output="" | |
# check each staged file via biome check | |
# @see https://biomejs.dev/reference/cli/ | |
for file in $staged_files; do | |
# check if file is also unstaged | |
if echo "$unstaged_files" | grep -q "$file"; then | |
# check only staged content with lint | |
file_content=$(git show ":$file") | |
err_output="$err_output"`echo "$file_content" | ./node_modules/.bin/biome lint --no-errors-on-unmatched --stdin-file-path="$file" 2>&1 > /dev/null` | |
continue | |
fi | |
# make full check when file is only staged | |
check_output=`./node_modules/.bin/biome check --no-errors-on-unmatched "$file" 2>&1` | |
if [ $? -ne 0 ]; then | |
err_output="$err_output$check_output" | |
fi | |
done | |
if [ -n "$err_output" ]; then | |
echo "$err_output" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment