Skip to content

Commit

Permalink
improve prettier formatting pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
arbakker committed Feb 2, 2024
1 parent b8c4fd3 commit 01ba637
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
42 changes: 18 additions & 24 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,29 @@ then
exit 0
fi

PY_FILES=$(git ls-files --cached --modified --other --exclude-standard | { grep -E "\.py$" || test $? = 1; })
YAML_FILES=$(git ls-files --cached --modified --other --exclude-standard | { grep -E "\.ya?ml$" || test $? = 1; })
print_message "start pre-commit hook" 1 "WARNING" true

if [ -n "$PY_FILES" ]; then
command_available "mypy"
print_message "running mypy static type checking" 1 "" true
if ! mypy src;then
print_message "mypy static type check failed - commit aborted" 1 "ERROR" true
exit 1
fi
command_available "mypy"
print_message "running mypy static type checking" 2 "" false
if ! mypy src;then # in if, otherwise script exits early due to set -eu
print_message "mypy static type check failed - commit aborted" 2 "ERROR" true
exit 1
fi

command_available "black"
print_message "running Black formatting" 1 "" true
black .
command_available "black"
print_message "running Black formatting" 2 "" true
black .

command_available "ruff"
print_message "running ruff linting" 1 "" true
ruff --fix .
command_available "ruff"
print_message "running ruff linting" 2 "" true
ruff --fix .

echo "$PY_FILES" | xargs git add
fi
command_available "prettier"
print_message "running prettier formatting" 2 "" true
prettier "**/*.(json|yaml|md)" -w --prose-wrap always --list-different

if [ -n "$YAML_FILES" ]; then
command_available "prettier"
print_message "running prettier yaml formatting" 1 "" true
echo "$YAML_FILES" | xargs prettier --ignore-unknown --stdin-filepath --write > /dev/null
echo "$YAML_FILES" | xargs git add
fi
git add --update # only add files that already exist; --update: Update the index just where it already has an entry matching <pathspec>

[[ -z $YAML_FILES ]] && [[ -z $PY_FILES ]] && print_message "no python and yaml files in commit to format and lint" -1 "" true && exit 0

print_message "pre-commit hook ran succesfully" 1 "INFO" true
exit 0
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore
# By default prettier ignores files in version control systems directories
# Prettier will also follow rules specified in the ".gitignore" file if it exists in the same directory from which it is run.
21 changes: 0 additions & 21 deletions .vscode/settings.json

This file was deleted.

0 comments on commit 01ba637

Please sign in to comment.