# This workflow will run tests using Node.js and publish a package to the npm registry when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
  release:
    types: [created]

permissions:
  id-token: write # Required for OIDC
  contents: read

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org/
      - uses: jaywcjlove/github-action-package@main
        with:
          version: ${{ github.ref_name }}
          rename: "@gofynd/fdk-store-gql"
      - run: npm ci
      - name: Publish to npm
        run: |
          VERSION=$(node -p "require('./package.json').version")
          echo "Publishing npm package version: $VERSION"
          if [[ "$VERSION" == *"-"* ]]; then
            echo "Prerelease detected. Publishing with npm dist-tag: beta"
            npm publish --access public --tag beta
          else
            echo "Stable release detected. Publishing with npm dist-tag: latest"
            npm publish --access public
          fi