#!/usr/bin/env bash

case $(uname) in
    *CYGWIN*|*MINGW*|*MSYS*)
        echo "Cygwin/MinGW/MSYS detected, skipping pre-commit hook"
        exit 0
        ;;
esac

echo "Running pre-commit hook..."

source ./scripts/common.sh

jsfiles=$(cachedTSLintFiles)
otherfiles=$(cachedPrettierFiles)

# Exit early if no relevant files are staged
if [ -z "$jsfiles" ] && [ -z "$otherfiles" ]; then
    exit 0
fi

echo "Running pre-commit hook..."

# Check if any of the files are ugly or contain a console.log call
consoleFiles=$(noisy $jsfiles)

if [ -n "$consoleFiles" ]; then
    echo "Warning: adding console.log calls in ${consoleFiles[@]}"
    echo 'Did you mean to use logger.debug?'
    echo
fi

# # disabled because it was too noisy
#
# if [ -n "$jsfiles" ]; then
#     yarn git-format-staged \
#       --formatter "yarn --silent prettier --stdin-filepath \"{}\"" \
#       "*.ts" "*.tsx"
# fi

# if [ -n "$otherfiles" ]; then
#     yarn git-format-staged \
#       --formatter "yarn --silent prettier --stdin-filepath \"{}\"" \
#       "*.md" "*.css"
# fi

if [ -n "$jsfiles" ]; then
    echo "Linting staged files..."
    if ! yarn eslint --rulesdir custom-eslint-rules --quiet $jsfiles; then
        echo ""
        echo -e "eslint failed. Please fix the errors above."
        echo 'If you see this message repeatedly, skip the check with git commit -n'
        exit 1
    fi
fi
