name: Build and publish PR

on:
  pull_request:
    branches: [ "main" ]
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          registry-url: 'https://registry.npmjs.org'
          cache: 'yarn'
          scope: '@talkamatic'
      - run: yarn
      - run: yarn compile
      - run: yarn build
      - run: npm version prerelease --no-git-tag-version --preid=${{ format('pr{0}-{1}-{2}', github.event.number, github.run_attempt, github.run_id) }}
      - name: Publish to NPM registry
        run: npm publish --tag ${{ format('pr{0}', github.event.number) }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
