name: Publish npm package

on:
  push:
    tags:
      - "v*"

jobs:
  publish:
    runs-on: x86

    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 24
          registry-url: "https://registry.npmjs.org"

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Check tag matches package version
        run: |
          PACKAGE_VERSION="v$(node -p "require('./package.json').version")"
          TAG_NAME="${GITHUB_REF_NAME:-${GITHUB_REF#refs/tags/}}"
          test "$PACKAGE_VERSION" = "$TAG_NAME"

      - name: Build
        run: bun run build

      - name: Publish
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
