#!/bin/bash
# Note: package registries are read from the .npmrc in the project root.
PACKAGE_NAME='@orchard/frontend-audio-player'

echo "looking for the latest version..."
LATEST_VERSION=$(
    yarn info $PACKAGE_NAME version --json \
    | grep -o '"data": *"[^"]*"' \
    | grep -o '"[^"]*"$'
)

LOCAL_VERSION=$(grep -o '"version": *"[^"]*"' package.json | grep -o '"[^"]*"$')
if [ ! $LOCAL_VERSION ]; then
    exit -1
fi

echo "the latest version is $LATEST_VERSION"
echo "your version is $LOCAL_VERSION"

if [ $LATEST_VERSION == $LOCAL_VERSION ]; then
    echo "version not changed, skipping..."
    exit 0
else
    echo "version changed, publishing new version..."

    yarn build-esm
    yarn publish --no-git-tag-version --no-commit-hooks --non-interactive
fi
