pre-commit 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. echo "Running unit tests check"
  42. modified_files=$(git diff --cached --name-only -- utils | grep -v '\.spec\.ts$' || true)
  43. if [ -n "$modified_files" ]; then
  44. for file in $modified_files; do
  45. test_file="${file%.*}.spec.ts"
  46. echo "Checking for test file: $test_file"
  47. # check if the test file exists
  48. if [ -f "../$test_file" ]; then
  49. echo "Detected changes in $file, running corresponding unit tests..."
  50. npm run test "../$test_file"
  51. if [ $? -ne 0 ]; then
  52. echo "Unit tests failed. Please fix the errors before committing."
  53. exit 1
  54. fi
  55. echo "Unit tests for $file passed."
  56. else
  57. echo "Warning: $file does not have a corresponding test file."
  58. fi
  59. done
  60. echo "All unit tests for modified web/utils files have passed."
  61. fi
  62. cd ../
  63. fi