#!/usr/bin/env bash

# Monitors your tree and runs tests when anything changes.
#
# `make`, `yarn test`, etc. automatically detected.
#
#   $ autotest
#
# Run a specific command on changes:
#
#   $ autotest mocha test/foo_test.js
#

trap exit INT

if [[ -f package.json ]]; then
  BUILD=${@:-yarn test}
elif [[ -f Makefile ]]; then
  BUILD=${@:-make}
else
  echo "Don't know what command to run. Try: autotest [command]" >&2
  exit 1
fi

while true; do
  ag -l | td entr -rd sh -c "echo ===; $BUILD"
done