name: Publish to npm

# Triggered by publishing a GitHub Release. Bump the version in package.json
# (and commit it) before creating the release — npm publishes whatever
# version package.json has, not the release tag.
on:
  release:
    types: [published]
  workflow_dispatch: {}

permissions:
  contents: read
  id-token: write # required for npm Trusted Publishing (OIDC) — no NPM_TOKEN secret needed

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

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci

      # Trusted Publishing requires npm CLI >= 11.5.1; pin explicitly so a
      # runner with an older bundled npm doesn't silently fall back/fail.
      - name: Ensure npm >= 11.5.1
        run: npm install -g npm@latest

      - name: Build
        run: npm run build

      - name: Test
        run: npm test

      - name: Publish to npm
        run: npm publish --provenance
