# This workflow publishes the package to npm when a release is created.
# Authentication uses npm trusted publishing (OIDC) — no NPM_TOKEN needed.
# For more information see: https://docs.npmjs.com/trusted-publishers

name: Node.js Package

on:
  release:
    types: [created]

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    permissions:
      contents: write # push the version bump commit back to main
      id-token: write # mint the OIDC token npm exchanges for publish rights
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org/

      # Trusted publishing requires npm >= 11.5.1
      - run: npm install -g npm@latest

      - name: Set env
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - run: |
          sudo apt update
          sudo apt install jq -y

      - run: |
          git config --local user.email "melvin.vermeer@gmail.com"
          git config --local user.name "Melvin Vermeer [ci]"
          git fetch
          git checkout main
          jq --arg version ${{ env.RELEASE_VERSION }} '.version = $version' package.json > package.json.tmp
          mv package.json.tmp package.json
          git add package.json
          git commit --no-verify -m "update package version ${{ env.RELEASE_VERSION }} [skip ci]"

      - run: npm publish

      - run: git push origin main --no-verify
