pre-commit 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/sh
  2. # get the list of modified files
  3. files=$(git diff --cached --name-only)
  4. # check if api or web directory is modified
  5. api_modified=false
  6. web_modified=false
  7. for file in $files
  8. do
  9. if [[ $file == "api/"* && $file == *.py ]]; then
  10. # set api_modified flag to true
  11. api_modified=true
  12. elif [[ $file == "web/"* ]]; then
  13. # set web_modified flag to true
  14. web_modified=true
  15. fi
  16. done
  17. # run linters based on the modified modules
  18. if $api_modified; then
  19. echo "Running Ruff linter on api module"
  20. # python style checks rely on `ruff` in path
  21. if ! command -v ruff &> /dev/null; then
  22. echo "Installing linting tools (Ruff, dotenv-linter ...) ..."
  23. poetry install -C api --only lint
  24. fi
  25. # run Ruff linter auto-fixing
  26. ruff check --fix ./api
  27. # run Ruff linter checks
  28. ruff check ./api || status=$?
  29. status=${status:-0}
  30. if [ $status -ne 0 ]; then
  31. echo "Ruff linter on api module error, exit code: $status"
  32. echo "Please run 'dev/reformat' to fix the fixable linting errors."
  33. exit 1
  34. fi
  35. fi
  36. if $web_modified; then
  37. echo "Running ESLint on web module"
  38. cd ./web || exit 1
  39. lint-staged
  40. echo "Running unit tests check"
  41. modified_files=$(git diff --cached --name-only -- utils | grep -v '\.spec\.ts$' || true)
  42. if [ -n "$modified_files" ]; then
  43. for file in $modified_files; do
  44. test_file="${file%.*}.spec.ts"
  45. echo "Checking for test file: $test_file"
  46. # check if the test file exists
  47. if [ -f "../$test_file" ]; then
  48. echo "Detected changes in $file, running corresponding unit tests..."
  49. pnpm run test "../$test_file"
  50. if [ $? -ne 0 ]; then
  51. echo "Unit tests failed. Please fix the errors before committing."
  52. exit 1
  53. fi
  54. echo "Unit tests for $file passed."
  55. else
  56. echo "Warning: $file does not have a corresponding test file."
  57. fi
  58. done
  59. echo "All unit tests for modified web/utils files have passed."
  60. fi
  61. cd ../
  62. fi