name: Publish to npm

on:
  release:
    types: [published]

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.release.tag_name }}
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - name: Install npm with trusted publishing support
        run: npm install --global 'npm@^11.5.1'
      - name: Verify release tag matches package version
        env:
          RELEASE_TAG: ${{ github.event.release.tag_name }}
        run: |
          node --input-type=module -e '
            import { readFileSync } from "node:fs";
            const { version } = JSON.parse(readFileSync("package.json", "utf8"));
            const expected = `v${version}`;
            if (process.env.RELEASE_TAG !== expected) {
              console.error(`Release tag must be ${expected}; got ${process.env.RELEASE_TAG}`);
              process.exit(1);
            }
          '
      - run: npm ci
      - run: npm run typecheck
      - run: npm test
      - run: npm run eval:offline
      - name: Publish with provenance
        run: npm publish --provenance --access public
