name: Release

on:
  pull_request:
    branches:
      - master
  push:
    branches:
      - master

permissions:
  contents: write
  pull-requests: write

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm i -g pnpm
      - run: pnpm run ci
      - run: pnpm test

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
      - run: npm i -g pnpm
      - run: pnpm run ci
      - run: pnpm build

  release:
    name: Release Please
    if: github.event_name == 'push'
    needs:
      - build
      - test
    runs-on: ubuntu-latest
    outputs:
      release_created: ${{ steps.release.outputs.release_created }}
    steps:
      - uses: googleapis/release-please-action@v4
        id: release
        with:
          token: ${{ secrets.RELEASE_PLEASE_TOKEN }}
          release-type: node
          target-branch: master

  publish:
    name: NPM Publish
    needs: release
    runs-on: ubuntu-latest
    if: ${{ needs.release.outputs.release_created }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: "https://registry.npmjs.org"
      - run: npm i -g pnpm
      - run: pnpm run ci
      - run: pnpm build
      - run: cd dist && npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
