name: coverall

on:
    push:
        branches:
            - main

jobs:
    test:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                app:
                    - api
                    - dashboard

        steps:
            - uses: actions/checkout@v2

            - name: Install Node.js
              uses: actions/setup-node@v1
              with:
                  node-version: 16.x

            - name: Get yarn cache directory path
              id: yarn-cache-dir-path
              run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

            - name: Restore yarn cache
              uses: actions/cache@v3
              id: yarn-cache
              with:
                  path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
                  key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
                  restore-keys: |
                      ${{ runner.os }}-yarn-

            - name: Install dependencies
              run: yarn

            - name: Test libraries
              run: yarn test ${{ matrix.app }} --coverage

            - name: Coveralls
              uses: coverallsapp/github-action@master
              with:
                  github-token: ${{ secrets.GITHUB_TOKEN }}
                  flag-name: run-${{ matrix.app }}
                  path-to-lcov: ./coverage/${{ matrix.app }}/lcov.info
                  parallel: true

    finish:
        needs: test
        runs-on: ubuntu-latest
        steps:
            - name: Coveralls Finished
              uses: coverallsapp/github-action@master
              with:
                  github-token: ${{ secrets.GITHUB_TOKEN }}
                  parallel-finished: true
