#!/bin/bash

# ==========================================
# Fast Silent Auto Publisher
# macOS
# ==========================================

BASE_NAME="ratelimitsucks"

while true
do

  # Get current package name
  CURRENT_NAME=$(node -p "require('./package.json').name")

  # Extract ending number
  NUMBER=$(echo "$CURRENT_NAME" | grep -o '[0-9]*$')

  # Increment
  if [ -z "$NUMBER" ]; then
    NEXT=1
  else
    NEXT=$((NUMBER + 1))
  fi

  NEW_NAME="${BASE_NAME}${NEXT}"

  # Update package name
  sed -i '' "s/\"name\": \".*\"/\"name\": \"$NEW_NAME\"/" package.json

  # Fast version bump
  npm version patch --no-git-tag-version > /dev/null 2>&1

  # Silent publish
  npm publish --access public --silent > /dev/null 2>&1

  # Print only success line
  echo "Published: $NEW_NAME"

done