#!/usr/bin/env bash

set -euo pipefail
bright_red='\u001b[31;1m'
bright_green='\u001b[32;1m'
reset_color='\033[0m'

# version read in from the package.json
tag="puffin__$(node -e "console.log(require('./package.json').version);")"

# Uncommited changes are not allowed.
if [[ -n $(git status -s) ]]
then
  echo -e "${bright_red}Please stash uncommited changes. ${reset_color}"
  exit 1;
fi

# Prevent overwriting build versions.
if [[ -n $(git ls-remote origin refs/tags/${tag}) ]]
then
  echo -e "${bright_red}Tagged version already exists on git. ${reset_color}"
  exit 1;
fi

# Ensure credentials are present before deploying.
ansible-playbook ansible/dev.yml --tags=tokens

# Unit tests for app should be passing.
yarn install --frozen-lockfile
yarn format
yarn unit
yarn build
# Pushes the build to AWS S3.
# Now that a build passes tests and has been built, tag it in git to ensure
# the version is immutable.
git tag ${tag}
git push origin ${tag}
yarn release
echo -e "${bright_green}Success! ${reset_color}"
