name: React Compiler (TypeScript)

on:
  push:
    branches: [main]
  pull_request:
    paths:
      - "compiler/**"
      - .github/workflows/compiler-typescript.yml

defaults:
  run:
    working-directory: compiler

jobs:
  discover_yarn_workspaces:
    name: Discover yarn workspaces
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v4
      - id: set-matrix
        run: echo "matrix=$(find packages -mindepth 1 -maxdepth 1 -type d | sed 's!packages/!!g' | tr '\n' ',' | sed s/.$// | jq -Rsc '. / "," - [""]')" >> $GITHUB_OUTPUT

  # Hardcoded to improve parallelism for babel-plugin-react-compiler
  prettier:
    name: Run prettier
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18.x
          cache: "yarn"
          cache-dependency-path: compiler/yarn.lock
      - name: Restore cached node_modules
        uses: actions/cache@v4
        with:
          path: "**/node_modules"
          key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('compiler/**/yarn.lock') }}
      - run: yarn install --frozen-lockfile
      - run: yarn prettier:ci

  # Hardcoded to improve parallelism
  lint:
    name: Lint babel-plugin-react-compiler
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18.x
          cache: "yarn"
          cache-dependency-path: compiler/yarn.lock
      - name: Restore cached node_modules
        uses: actions/cache@v4
        with:
          path: "**/node_modules"
          key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('compiler/**/yarn.lock') }}
      - run: yarn install --frozen-lockfile
      - run: yarn workspace babel-plugin-react-compiler lint

  # Hardcoded to improve parallelism
  jest:
    name: Jest babel-plugin-react-compiler
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18.x
          cache: "yarn"
          cache-dependency-path: compiler/yarn.lock
      - name: Restore cached node_modules
        uses: actions/cache@v4
        id: node_modules
        with:
          path: "**/node_modules"
          key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('compiler/**/yarn.lock') }}
      - run: yarn install --frozen-lockfile
      - run: yarn workspace babel-plugin-react-compiler jest

  test:
    name: Test ${{ matrix.workspace_name }}
    needs: discover_yarn_workspaces
    runs-on: ubuntu-latest
    continue-on-error: true
    strategy:
      matrix:
        workspace_name: ${{ fromJSON(needs.discover_yarn_workspaces.outputs.matrix) }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 18.x
          cache: "yarn"
          cache-dependency-path: compiler/yarn.lock
      - name: Restore cached node_modules
        uses: actions/cache@v4
        id: node_modules
        with:
          path: "**/node_modules"
          key: ${{ runner.arch }}-${{ runner.os }}-modules-${{ hashFiles('compiler/**/yarn.lock') }}
      - run: yarn install --frozen-lockfile
      - run: yarn workspace ${{ matrix.workspace_name }} test
