#!/bin/sh

COMMITTED_FILES() {
  # get filenames of committed but not pushed files on current branch
  git diff --staged --name-only --diff-filter=d "$(git remote)/$(git branch --show-current)" --
}
COMMITTED_TS_FILES=$(COMMITTED_FILES | grep '\.tsx\?$')
BIN_PATH="$(git rev-parse --show-toplevel)/node_modules/.bin"

if [ "$COMMITTED_TS_FILES" ]; then
  RED="\033[1;31m"
  GREEN="\033[1;32m"
  NC="\033[0m"

  echo "eslint command $BIN_PATH/eslint $COMMITTED_TS_FILES --fix --max-warnings 0"
  "$BIN_PATH"/eslint "$COMMITTED_TS_FILES" --fix --max-warnings 0
  linter_exit_code=$?

  if [ $linter_exit_code -eq 1 ]; then
    echo "${RED} ❌ Eslint errors have occurred ${NC}"
    exit 1
  else
    echo "${GREEN} ✔ Eslint did not find any errors ${NC}"
  fi

# currently there are no tests in micro projects
#  "$BIN_PATH"/jest -o
#  jest_exit_code=$?
#
#  if [ $jest_exit_code -ne 0 ]; then
#    echo "${RED} ❌ Jest errors have occurred ${NC}"
#    exit 1
#  else
#    echo "${GREEN} ✔ Jest did not find any errors ${NC}"
#    exit 0
#  fi
else
    echo "no files found"
fi
