Underground-Digital/Workflow-Engine
0
1#!/usr/bin/env bash2. "$(dirname -- "$0")/_/husky.sh"3 4# get the list of modified files5files=$(git diff --cached --name-only)6 7# check if api or web directory is modified8 9api_modified=false10web_modified=false11 12for file in $files13do14 if [[ $file == "api/"* && $file == *.py ]]; then15 # set api_modified flag to true16 api_modified=true17 elif [[ $file == "web/"* ]]; then18 # set web_modified flag to true19 web_modified=true20 fi21done22 23# run linters based on the modified modules24 25if $api_modified; then26 echo "Running Ruff linter on api module"27 28 # python style checks rely on `ruff` in path29 if ! command -v ruff &> /dev/null; then30 echo "Installing linting tools (Ruff, dotenv-linter ...) ..."31 poetry install -C api --only lint32 fi33 34 # run Ruff linter auto-fixing35 ruff check --fix ./api36 37 # run Ruff linter checks38 ruff check --preview ./api || status=$?39 40 status=${status:-0}41 42 43 if [ $status -ne 0 ]; then44 echo "Ruff linter on api module error, exit code: $status"45 echo "Please run 'dev/reformat' to fix the fixable linting errors."46 exit 147 fi48fi49 50if $web_modified; then51 echo "Running ESLint on web module"52 cd ./web || exit 153 npx lint-staged54 55 echo "Running unit tests check"56 modified_files=$(git diff --cached --name-only -- utils | grep -v '\.spec\.ts$' || true)57 58 if [ -n "$modified_files" ]; then59 for file in $modified_files; do60 test_file="${file%.*}.spec.ts"61 echo "Checking for test file: $test_file"62 63 # check if the test file exists64 if [ -f "../$test_file" ]; then65 echo "Detected changes in $file, running corresponding unit tests..."66 npm run test "../$test_file"67 68 if [ $? -ne 0 ]; then69 echo "Unit tests failed. Please fix the errors before committing."70 exit 171 fi72 echo "Unit tests for $file passed."73 else74 echo "Warning: $file does not have a corresponding test file."75 fi76 77 done78 echo "All unit tests for modified web/utils files have passed."79 fi80 81 cd ../82fi83 