#!/bin/sh

# RUN PRETTIER
filesToPrettify=$(git diff --staged --name-only | grep ".*\.\(js\|json\|css\|md\)")
echo "$fileToPrettify" | xargs ./node_modules/.bin/prettier --config ./prettierrc --write
echo "$fileToPrettify" | xargs git add

# Fetch .js or .json filed from staged files
jsfiles=$(git diff --staged --name-only --diff-filter=ACM | grep '\.js$')

[ -z "$jsfiles" ] && exit 0

# ESLINT CHECK
eslintresult=$(./node_modules/.bin/eslint --ignore-pattern '/src/lib/*' --fix --color $jsfiles --quiet)

if [[ $eslintresult != "" ]]; then
	echo "$eslintresult"
	exit 1 # reject
fi

echo "\033[32m✔ ESlint passed\033[0m"

exit 0
