name: Publish to npm

env:
  NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
  GH_TOKEN: ${{ github.token }}

on:
  push:
    branches:
      - master

jobs:
  publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'

      - name: Install Dependencies
        run: npm install

      - name: Login to npm
        run: echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }}" > ~/.npmrc

      - name: Create Tagged Release and Push to GitHub
        run: |
          PACKAGE_VERSION=$(node -p "require('./package.json').version")
          git config --local user.email "tannermichaelgaucher@gmail.com"
          git config --local user.name "Tanner Gaucher"
          git tag -a "v$PACKAGE_VERSION" -m "v$PACKAGE_VERSION"
          git push --follow-tags
          gh release create "v$PACKAGE_VERSION"

      - name: Publish to npm
        run: |
          # Run npm publish with appropriate options
          npm publish --access public  # You can adjust this command based on your npm package configuration
