name: Publish to npm

on:
  push:
    tags:
      - 'v*.*.*'

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    name: Publish package
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10.33.0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: https://registry.npmjs.org
          cache: pnpm

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

      - name: Check tag version
        run: |
          TAG_VERSION="${GITHUB_REF_NAME#v}"
          PACKAGE_VERSION="$(node -p "require('./package.json').version")"

          if [ "$TAG_VERSION" != "$PACKAGE_VERSION" ]; then
            echo "Tag version v$TAG_VERSION does not match package.json version $PACKAGE_VERSION."
            exit 1
          fi

      - name: Lint
        run: pnpm lint

      - name: Build plugin
        run: pnpm build:plugin

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