#!/usr/bin/env bash

set -e

echo "Running dev (don't forget to 'yarn make-assets' if you've changed GraphiQL / favicon / etc)..."
cd $(dirname $0)/..


export POSTGRAPHILE_ENV=development
export BROWSER=none

# Run Nodemon and watch the PostGraphile source code for changes.
if [ "$1" = "--" ]; then
  if [ "$DEBUG" = "" ]; then
    echo 'You might want to:'
    echo
    echo '  export DEBUG="postgraphile*,graphile-build:warn,-postgraphile:graphql,-postgraphile:postgres:explain"'
  fi
  shift
  npx --no-install ts-node --transpile-only src/postgraphile/cli.ts "$@" &
else
  export DEBUG=${DEBUG-postgraphile*,graphile-build:warn,-postgraphile:graphql,-postgraphile:postgres:explain}
  npx --no-install nodemon \
    --watch ../packages/postgraphile-core/node8plus \
    --watch ../packages/graphile-build-pg/node8plus \
    --watch ../packages/graphile-build/node8plus \
    --watch ../packages/graphile-utils/node8plus \
    --watch src \
    --ignore __tests__ \
    --ignore __mocks__ \
    --ignore src/postgraphile/graphiql \
    --ext js,ts \
    --exec "npx --no-install ts-node --transpile-only src/postgraphile/cli.ts $* --show-error-stack json --extended-errors hint,detail,errcode" &
fi;

# Ensure forked process is killed even if we die unexpectedly
trap 'trap - SIGINT SIGTERM EXIT; JOBS="$(jobs -p)"; [[ "$JOBS" != "" ]] && kill $JOBS 2>/dev/null || true' SIGINT SIGTERM EXIT

# Run `react-scripts` in the GraphiQL directory as well parallel, but pipe the
# output to `/dev/null`.
(sleep 1 && cd postgraphiql && PORT=5783 node_modules/.bin/react-scripts start) > /dev/null &

wait %1
kill %2
wait
