name: release

on:
  push:
    branches: next
  pull_request:
    branches: next

jobs:
  install:
    name: Cache node_modules
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive

      - name: Cache node modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: |
            node_modules
            */*/node_modules
          key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: Cache Cypress binary
        id: cache-cypress-binary
        uses: actions/cache@v2
        with:
          path: ~/.cache/Cypress
          key: cypress-${{ runner.os }}-cache-${{ hashFiles('**/yarn.lock') }}

      - name: Install
        env:
          # make sure every Cypress install prints minimal information
          CI: 1
        if: |
          steps.cache-node-modules.outputs.cache-hit != 'true' ||
          steps.cache-cypress-binary.outputs.cache-hit != 'true'
        run: |
          corepack enable
          yarn workspaces foreach install

  # cypress-run:
  #   name: Cypress run
  #   runs-on: ubuntu-latest
  #   needs: install
  #   steps:
  #     - uses: actions/checkout@v2

  #     - name: Load node_modules
  #       uses: actions/cache@v2
  #       with:
  #         path: |
  #           node_modules
  #           */*/node_modules
  #         key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

  #     - name: Cache Cypress binary
  #       id: cache-cypress-binary
  #       uses: actions/cache@v2
  #       with:
  #         path: ~/.cache/Cypress
  #         key: cypress-${{ runner.os }}-cache-${{ hashFiles('**/yarn.lock') }}

  #     - name: Install dependencies
  #       if: steps.cache-cypress-binary.outputs.cache-hit != 'true'
  #       run: |
  #         yarn install --frozen-lockfile
  #         npx cypress install

  #     - name: Verify Cypress
  #       run: |
  #         npx cypress cache path
  #         npx cypress cache list
  #         npx cypress verify

  #     - name: Start server in the background
  #       run: yarn dev:web &

  #     - name: Cypress tests
  #       uses: cypress-io/github-action@v2
  #       env:
  #         CYPRESS_ACTION: true
  #       with:
  #         install: false
  #         start: yarn dev:api
  #         command: yarn test:run

  #     - name: Compress and upload the test result folder
  #       if: always()
  #       env:
  #         ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
  #       run: |
  #         tar -zcvf ./tests/e2e/video.tar.gz ./tests/e2e/video
  #         ./script/deploy.sh $ACCESS_TOKEN github.com/hq001/test_video.git tests/e2e

  lint:
    name: Lint code
    runs-on: ubuntu-latest
    needs: install
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive

      - uses: actions/setup-node@v1
        with:
          node-version: 16

      - name: Load node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: |
            node_modules
            */*/node_modules
          key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: Install
        if: steps.cache-node-modules.outputs.cache-hit != 'true'
        run: |
          corepack enable 
          yarn workspaces foreach install

      - name: Lint
        run: |
          corepack enable 
          yarn lint

  build-electron:
    name: Deploy Radishes
    strategy:
      matrix:
        os: [macos-latest, windows-latest, ubuntu-latest]
    runs-on: ${{ matrix.os }}
    needs: install
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive

      - uses: actions/setup-node@v1
        with:
          node-version: 16

      # - name: Load node_modules
      #   id: cache-node-modules
      #   uses: actions/cache@v2
      #   with:
      #     path: |
      #       node_modules
      #       */*/node_modules
      #     key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

      - name: Install
        # if: steps.cache-node-modules.outputs.cache-hit != 'true'
        run: |
          corepack enable
          yarn workspaces foreach install

      - name: Build electron win
        if: matrix.os == 'windows-latest'
        run: |
          corepack enable
          yarn pkg:win
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build electron linux
        if: matrix.os == 'ubuntu-latest'
        run: |
          corepack enable
          yarn pkg:linux
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build electron macos
        if: matrix.os == 'macos-latest'
        run: |
          corepack enable
          yarn pkg:macos
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Releases to github
        run: |
          echo 'Waiting for scripting'