name: Publish dev
on:
  schedule:
    - cron: '0 */12 * * *'
  workflow_dispatch:
    inputs:
      ref:
        description: 'The ref to check out. e.g. main, feat/new-feature, refs/pull/1234/head'
        required: false
        default: 'main'
      tag:
        description: 'The tag to use, generally a feature name'
        required: false
        type: string
      dry_run:
        description: 'Perform a dry run that skips publishing and outputs logs indicating what would have happened'
        type: boolean
        default: false
jobs:
  npm-publish:
    name: npm publish
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    env:
      TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
      TURBO_TEAM: ${{ vars.TURBO_TEAM }}
    if: github.repository_owner == 'discordjs'
    steps:
      - uses: actions/create-github-app-token@v2
        id: app-token
        with:
          app-id: ${{ vars.DISCORDJS_APP_ID }}
          private-key: ${{ secrets.DISCORDJS_APP_KEY_RELEASE }}

      - name: Checkout repository
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          token: ${{ steps.app-token.outputs.token }}
          ref: ${{ inputs.ref }}

      - name: Install Node.js v24
        uses: actions/setup-node@v6
        with:
          node-version: 24
          package-manager-cache: false
          registry-url: https://registry.npmjs.org/

      - name: Install dependencies
        uses: ./packages/actions/src/pnpmCache

      - name: Build dependencies
        run: pnpm run build

      - name: Checkout main repository (non-main ref)
        if: ${{ inputs.ref != 'main' }}
        uses: actions/checkout@v6
        with:
          path: 'main'

      - name: Install action deps (non-main ref)
        if: ${{ inputs.ref != 'main' }}
        shell: bash
        working-directory: ./main
        env:
          COREPACK_ENABLE_STRICT: 0
        run: |
          pnpm self-update 10
          pnpm install --filter @discordjs/actions --frozen-lockfile --prefer-offline --loglevel error

      - name: Publish packages (non-main ref)
        if: ${{ inputs.ref != 'main' }}
        uses: ./main/packages/actions/src/releasePackages
        with:
          exclude: '@discordjs/docgen'
          dry: ${{ inputs.dry_run }}
          dev: true
          tag: ${{ inputs.tag || 'dev' }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Publish packages (main ref)
        if: ${{ inputs.ref == 'main' }}
        uses: ./packages/actions/src/releasePackages
        with:
          exclude: '@discordjs/docgen'
          dry: ${{ inputs.dry_run }}
          dev: true
          tag: ${{ inputs.tag || 'dev' }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
