pre-commit 2.1 KB

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