#!/usr/bin/env bash

# Go up the directory tree until you arrive to the git root
while [ ! -d .git ]
do
  cd ../
done


files=$(git diff --cached --name-only --diff-filter=ACM | grep "^[src/|test/].*\.js$")

if [ "$files" = "" ]; then
    exit 0
fi

pass=true

echo -e "Validating JavaScript: \c"

for file in ${files}; do
    echo -e "Validating:\033[34m ${file}\033[0m\c"
    ./node_modules/.bin/jshint --verbose --reporter=node_modules/jshint-stylish ${file}

    if [ $? -ne 0 ]; then
        pass=false
    fi
done

echo -e "JavaScript validation complete"

if ! $pass; then
    echo -e "\033[41mCOMMIT FAILED:\033[0m Your commit contains files that should pass JSHint but do not. Please fix the JSHint errors and try again."
    exit 1
else
    echo -e "\033[42mCOMMIT SUCCEEDED\033[0m"
fi