From 01ba63794606a824ea3655d135d82fd95c5ab9bd Mon Sep 17 00:00:00 2001 From: Anton Bakker Date: Thu, 1 Feb 2024 17:41:10 +0100 Subject: [PATCH] improve prettier formatting pre-commit hook --- .githooks/pre-commit | 42 ++++++++++++++++++------------------------ .prettierignore | 3 +++ .vscode/settings.json | 21 --------------------- 3 files changed, 21 insertions(+), 45 deletions(-) create mode 100644 .prettierignore delete mode 100644 .vscode/settings.json diff --git a/.githooks/pre-commit b/.githooks/pre-commit index af06ac9..d272e92 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -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 -[[ -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 diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..e546a3b --- /dev/null +++ b/.prettierignore @@ -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. diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 76ee297..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "[python]": { - "editor.defaultFormatter": "ms-python.black-formatter" - }, - "python.testing.pytestArgs": [ - "tests" - ], - "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true, - "spellright.language": [ - "en" - ], - "spellright.documentTypes": [ - "latex", - "plaintext", - "markdown" - ], - "yaml.schemas": { - "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/schemas/v3.0/schema.json": "file:///home/anton/workspace/github.com/geodetischeinfrastructuur/coordinate-transformation-api/src/coordinate_transformation_api/assets/openapi.yaml" - } -}