name: Publish next

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  publish:
    concurrency:
      group: publish-next
      cancel-in-progress: true

    name: Publish @next release to npm
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    if: github.repository_owner == 'discordjs' && !(github.event_name == 'push' && startsWith(github.event.head_commit.message, 'chore(release)'))
    steps:
      # - name: Cancel previous publish attempts
      #   uses: styfle/cancel-workflow-action@0.12.1
      #   with:
      #     access_token: ${{ github.token }}

      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install Node v20
        uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: https://registry.npmjs.org/

      - name: Install dependencies
        run: npm ci

      - name: Bump version
        run: node ./scripts/bump-version.mjs

      - name: Deprecate old @next versions
        run: npx npm-deprecate --name "*next*" --package discord-api-types --message "No longer supported. Install the latest @next release" || true
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

      - name: Publish new @next version
        run: |
          npm version $(jq --raw-output '.version' package.json)-next.$(git rev-parse --short HEAD).$(date +%s)
          npm publish --tag next
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
