#!/usr/bin/env bash

set -eo pipefail

script_dir="$(dirname "$0")"

args=("$@")

# If the command is used like `yarn test plugin`, set the --rootDir option to the `plugin` directory
if [ "$1" == "plugin" ]; then
  args=()
  args+=("--rootDir")
  args+=("plugin")

  if [[ -f plugin/jest.config.js ]]; then
    args+=("--config")
    args+=("plugin/jest.config.js")
  fi

  # Push the rest of the arguments minus the `plugin` arg
  args+=("${@:2}")
fi

if [[ -t 1 && (-z "$CI" && -z "$EXPO_NONINTERACTIVE") ]]; then
  args+=("--watch")
fi

"$script_dir/expo-module-jest" "${args[@]}"
