name: backframe.js/ci

on:
  pull_request:
    branches:
      - main
      - dev-main

env:
  CI: true
  TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
  TURBO_TEAM: ${{ secrets.TURBO_TEAM }}
  PG_DATABASE_URL: ${{ secrets.PG_DATABASE_URL }}
  PRISMA_DATABASE_URL: ${{ secrets.PRISMA_DATABASE_URL }}
  BF_AUTH_SECRET: "somesecret"

jobs:
  ci:
    timeout-minutes: 15
    strategy:
      matrix:
        os: ["ubuntu-latest", "windows-latest", "macos-latest"]
        node: [16, 18, 20]

    name: "${{ matrix.os }} / Node.js ${{ matrix.node }}"
    runs-on: ${{ matrix.os }}

    steps:
      - name: checkout code repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - uses: pnpm/action-setup@v2
        name: Install pnpm
        id: pnpm-install
        with:
          version: 7
          run_install: false

      - name: Configure pnpm
        run: |
          pnpm config set git-checks false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v3
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - name: install dependencies
        run: pnpm install

      - name: build packages
        run: pnpm build

      - name: push db schema(s)
        run: pnpm turbo db:push

      - name: run tests
        run: pnpm run:tests
        shell: bash
