#!/bin/bash
set -e

CLI_SUCCESS="\033[1;32m✔"
CLI_ERROR="\033[1;31m✘"
CLI_QUERY="\033[1;36m→"
CLI_RESET="\033[0m"

read -p "Enter a new version (current: $(node -p 'require("./package.json").version')): " new_version

npm test

if [ -n "$new_version" ]
then
  npm version "$new_version"

  echo -e "$CLI_SUCCESS Bumped version to \"$(node -p 'require("./package.json").version')\"$CLI_RESET"
fi

version=$(node -p 'require("./package.json").version')

if [[ $(npm view gsheets@$version name) == "gsheets" ]]
then
  echo -e "$CLI_SUCCESS Version \"$version\" is already published. Aborting.$CLI_RESET"
  exit 0
fi

if [[ "$version" == *"-"* ]]
then
  tag="next"
else
  tag="latest"
fi

npm publish --tag $tag

echo -e "$CLI_SUCCESS Published version \"$version\" with tag \"$tag\" to npm$CLI_RESET"

exit 0
