name: Release

on:
  push:
    branches:
      - main
    paths:
      - packages/**
      - .github/**

jobs:
  path-filter:
    name: Check Path Changes
    runs-on: ubuntu-latest
    outputs:
      bot: ${ steps.changes.outputs.bot }
      gateway: ${ steps.changes.outputs.gateway }
      rest: ${ steps.changes.outputs.rest }
      types: ${ steps.changes.outputs.types }
      utils: ${ steps.changes.outputs.utils }
      global: ${ steps.changes.outputs.global }
    steps:
      - uses: actions/checkout@v3
      - uses: dorny/paths-filter@v2
        id: changes
        with:
          base: node-migration-clean
          filters: |
            gateway:
            - "packages/gateway/**"
            rest:
            - "packages/rest/**"
            types:
            - "packages/types/**"
            utils:
            - "packages/utils/**"
            bot:
            - "packages/bot/**"
            global:
            - ".github/**"

  publish:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    strategy:
      fail-fast: false
      matrix:
        package: ["gateway", "rest", "types", "utils", "bot"]
        run: ["false"]
        exclude:
          - package: gateway
            run: "${{ needs.path-filter.outputs.gateway == 'true' || needs.path-filter.outputs.global == 'true' }}"
          - package: rest
            run: "${{ needs.path-filter.outputs.rest == 'true' || needs.path-filter.outputs.global == 'true' }}"
          - package: types
            run: "${{ needs.path-filter.outputs.types == 'true' || needs.path-filter.outputs.global == 'true' }}"
          - package: utils
            run: "${{ needs.path-filter.outputs.utils == 'true' || needs.path-filter.outputs.global == 'true' }}"
          - package: bot
            run: "${{ needs.path-filter.outputs.bot == 'true' || needs.path-filter.outputs.global == 'true' }}"
    needs: path-filter
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
          registry-url: "https://registry.npmjs.org"
          scope: "@discordeno"
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT
      - uses: actions/cache@v3
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - run: yarn install --immutable
      - name: Turbo Cache
        id: turbo-cache
        uses: actions/cache@v3
        with:
          path: .turbo
          key: ${{ runner.os }}-turbo-release-build-${{ matrix.package }}-${{ github.sha }}
      - name: build Package
        run: yarn release-build --cache-dir=".turbo" --filter=./packages/${{ matrix.package }}
      - name: Bump Package Version
        run: node ./scripts/bumpVersionByCommit.js ${{ matrix.package }}

      - run: npm publish --access public
        working-directory: packages/${{ matrix.package }}
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
