name: Release
permissions:
  contents: read
  packages: write
  id-token: write
on:
  push:
    branches:
      - main
    paths:
      - packages/**
      - .github/**
  release:
      types: [published]
  workflow_dispatch:

jobs:
  publish:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package: ['gateway', 'rest', 'types', 'utils', 'bot', 'discordeno']
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 20
          cache: yarn
          registry-url: 'https://registry.npmjs.org'
          scope: '@discordeno'
      - run: yarn install --immutable
      - name: build Package
        run: yarn release-build --filter=./packages/${{ matrix.package }}
      - name: Bump Package Version
        if: ${{ github.event_name != 'release' && github.event.action != 'published' }}
        run: node ./scripts/bumpVersionByCommit.js ${{ matrix.package }}

      # If the action is running from a release, if prerelease use beta tag else use latest tag, otherwise use the next tag
      - run: yarn npm publish --tag ${{ github.event_name == 'release' && github.event.action == 'published' && (github.event.release.prerelease && 'beta' || 'latest') || 'next' }} --access public --provenance
        working-directory: packages/${{ matrix.package }}
