#!/bin/bash
set -e

BUILD_DEBUG="${BUILD_DEBUG:-}"
if [[ -n "${BUILD_DEBUG}" ]]; then
    set -x
    env
fi

# cd to app root
CWD=$(dirname $0)
if [[ `basename $(pwd)` = 'scripts' ]]; then
  cd ../
else
  cd `dirname $CWD`
fi

echo "Cleaning Up.."
rm -rf node_modules build dist tmp

echo "Bootstrapping.."
./scripts/bootstrap

echo "Linting.."
./node_modules/.bin/eslint --max-warnings 0 .

if [[ "${BUILD_LATEST}" == "true" ]]; then
  echo "Building..."
  ./scripts/build-static -l -v latest2
  # (build-static contains a call to test & build)
else
  echo "Testing.."
  yarn test
fi
