pre-commit 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env bash
  2. . "$(dirname -- "$0")/_/husky.sh"
  3. # get the list of modified files
  4. files=$(git diff --cached --name-only)
  5. # check if api or web directory is modified
  6. api_modified=false
  7. web_modified=false
  8. for file in $files
  9. do
  10. if [[ $file == "api/"* && $file == *.py ]]; then
  11. # set api_modified flag to true
  12. api_modified=true
  13. elif [[ $file == "web/"* ]]; then
  14. # set web_modified flag to true
  15. web_modified=true
  16. fi
  17. done
  18. # run linters based on the modified modules
  19. if $api_modified; then
  20. echo "Running Ruff linter on api module"
  21. # python style checks rely on `ruff` in path
  22. if ! command -v ruff &> /dev/null; then
  23. echo "Installing linting tools (Ruff, dotenv-linter ...) ..."
  24. poetry install -C api --only lint
  25. fi
  26. # run Ruff linter auto-fixing
  27. ruff check --fix ./api
  28. # run Ruff linter checks
  29. ruff check --preview ./api || status=$?
  30. status=${status:-0}
  31. if [ $status -ne 0 ]; then
  32. echo "Ruff linter on api module error, exit code: $status"
  33. echo "Please run 'dev/reformat' to fix the fixable linting errors."
  34. exit 1
  35. fi
  36. fi
  37. if $web_modified; then
  38. echo "Running ESLint on web module"
  39. cd ./web || exit 1
  40. npx lint-staged
  41. cd ../
  42. fi