#!/bin/bash
# Checks to run.
# Usable from both `pre-commit` and the commandline.

set -u

eslint_flags=()
if [[ -v GIT_AUTHOR_DATE ]]; then
  echo 'inside pre-commit'
else
  echo 'outside pre-commit'
  eslint_flags+=('--report-unused-disable-directives' '--fix')
fi

messages=()
# These are the tests we want to run.
spelling_errors=$(mktemp)
find . -type f \( -name '*.js' -o -name '*.md' -o -name '*.yml' \) \
  -exec cat {} + |
  ispell -o -l > "${spelling_errors}"

if [[ -s ${spelling_errors} ]]; then
  echo spelling errors:
  sort -u "${spelling_errors}"
  messages+=('spell checking failed')
fi

hooks/style_linter.py || messages+=('style linter failed')
eslint "${eslint_flags[@]}" . || messages+=('eslint failed')

cnt=${#messages[*]}

if ((cnt)); then
  echo "Issues found (${cnt}):"
  for msg in "${messages[@]}"; do
    echo "${msg}"
  done
  exit 1
fi
