# Automate, customize, and execute your software development workflows right in your repository with GitHub Actions.
# Documentation: https://docs.github.com/en/actions

name: build

env:
  PNPM_VERSION: 9.15.4

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - main
      - develop
      - next
      - release/*
    tags:
      - '*'
  pull_request:
    branches:
      - main
      - develop
      - next

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [20]

    steps:
      - uses: actions/checkout@v4.1.4

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4.1.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'pnpm'

      - name: Load turbo cache
        uses: actions/cache@v4.2.3
        id: turbo-cache
        with:
          path: |
            .turbo
            **/.eslintcache
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-${{ github.sha }}
            ${{ runner.os }}-turbo-

      - name: Install dependencies
        id: install-dependencies
        run: pnpm install

      - name: Lint code
        run: pnpm run lint

      - name: Try to build the packages
        id: build-packages
        run: pnpm run build

  test:
    runs-on: ubuntu-latest

    needs: build

    strategy:
      matrix:
        node-version: [20]
        test-spec:
          - { name: 'Integration', spec: './tests/cypress/integration/**/*.spec.{js,ts}' }
          - { name: 'Demos/Commands', spec: './demos/src/Commands/**/*.spec.{js,ts}' }
          - { name: 'Demos/Examples', spec: './demos/src/Examples/**/*.spec.{js,ts}' }
          - { name: 'Demos/Experiments', spec: './demos/src/Experiments/**/*.spec.{js,ts}' }
          - { name: 'Demos/Extensions', spec: './demos/src/Extensions/**/*.spec.{js,ts}' }
          - { name: 'Demos/GuideContent', spec: './demos/src/GuideContent/**/*.spec.{js,ts}' }
          - { name: 'Demos/GuideGettingStarted', spec: './demos/src/GuideGettingStarted/**/*.spec.{js,ts}' }
          #- { name: "Demos/GuideNodeViews", "./demos/src/GuideNodeViews/**/*.spec.{js,ts}" }
          - { name: 'Demos/Marks', spec: './demos/src/Marks/**/*.spec.{js,ts}' }
          - { name: 'Demos/Nodes', spec: './demos/src/Nodes/**/*.spec.{js,ts}' }
          #- { name: "Demos/Overview", spec: "./demos/src/Overview/**/*.spec.{js,ts}" }

    steps:
      - uses: actions/checkout@v4.1.4
      - name: Load turbo cache
        uses: actions/cache@v4.2.0
        id: turbo-cache
        with:
          path: |
            .turbo
            **/.eslintcache
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-${{ github.sha }}
            ${{ runner.os }}-turbo-
      - name: Load build cache
        uses: actions/cache@v4.2.0
        id: cache
        with:
          path: |
            ./demos/node_modules/.vite
            /home/runner/.cache/Cypress
          key: ${{ runner.os }}-build-${{ matrix.test-spec.name }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-build-${{ matrix.test-spec.name }}-${{ github.sha }}
            ${{ runner.os }}-build-${{ matrix.test-spec.name }}-

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4.1.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'pnpm'

      - name: Install dependencies
        id: install-dependencies
        run: pnpm install && pnpm exec cypress install

      - name: Try to build the packages
        id: build-packages
        run: pnpm run build

      - name: Test ${{ matrix.test-spec.name }}
        id: cypress
        uses: cypress-io/github-action@v6.7.16
        with:
          cache-key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/pnpm-lock.yaml') }}
          start: pnpm run serve
          wait-on: 'http://localhost:3000'
          spec: ${{ matrix.test-spec.spec }}
          project: ./tests
          browser: chrome
          quiet: true

      - name: Export screenshots (on failure only)
        uses: actions/upload-artifact@v4.6.2
        if: failure()
        with:
          name: cypress-screenshots
          path: tests/cypress/screenshots
          retention-days: 7

      - name: Export screen recordings (on failure only)
        uses: actions/upload-artifact@v4.6.2
        if: failure()
        with:
          name: cypress-videos
          path: tests/cypress/videos
          retention-days: 7

  release:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [20]

    needs: test

    steps:
      - uses: actions/checkout@v4.1.4

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: ${{ env.PNPM_VERSION }}

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4.1.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'pnpm'

      - name: Load turbo cache
        uses: actions/cache@v4.2.3
        id: turbo-cache
        with:
          path: |
            .turbo
            **/.eslintcache
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-${{ github.sha }}
            ${{ runner.os }}-turbo-

      - name: Install dependencies
        id: install-dependencies
        run: pnpm install

      - name: Try to build the packages
        id: build-packages
        run: pnpm run build

      - name: Soft release
        id: soft-release
        run: pnpm exec pkg-pr-new publish './packages/*' './packages-deprecated/*' --compact
