# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Publish to NPM

on:
  release:
    types: [created]

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "14"
          registry-url: "https://registry.npmjs.org/"

      - name: Install dependencies
        run: npm install

      - name: Check publishConfig registry
        id: check_registry
        run: |
          REGISTRY_URL=$(jq -r '.publishConfig.registry' package.json)
          echo "Registry URL: $REGISTRY_URL"
          if [[ "$REGISTRY_URL" != "https://registry.npmjs.org/" ]]; then
            echo "Registry URL is not valid. Skipping publish."
            echo "should_publish=false" >> $GITHUB_ENV
          else
            echo "should_publish=true" >> $GITHUB_ENV
          fi

      - name: Publish to npm
        if: env.should_publish == 'true'
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
