name: Node.js Package

on:
  release:
    types: [created]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: 18
      - name: Check for package-lock.json
        run: |
          if [ ! -f package-lock.json ]; then
            npm install
          fi
      - run: npm ci

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    environment: prod  # Specify the environment to access its secrets
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v3
        with:
          node-version: 18
          registry-url: https://registry.npmjs.org/
      - name: Configure npm for publishing
        run: |
          echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc
      - name: Display .npmrc for debugging
        run: cat ~/.npmrc  # Remove this after debugging
      - run: npm ci
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
