name: Frontend CI

on:
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x]

    env:
      CI: true
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - uses: actions/checkout@v2
      - name: fetch branch master
        run: git fetch origin master
      - name: find location of global yarn cache
        id: yarn-cache
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - name: cache global yarn cache
        uses: actions/cache@v2
        with:
          path: ${{ steps.yarn-cache.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - name: cache node_modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: ${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }}
      - name: use node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: check for yarn.lock changes
        id: yarn-lock
        run: git diff --quiet origin/master HEAD -- yarn.lock
        continue-on-error: true

      - name: yarn install
        run: yarn install --frozen-lockfile

      - name: lint
        run: yarn lerna -- run lint --since origin/master

      - name: type checking and declarations
        run: yarn tsc --incremental false

      - name: build changed packages
        if: ${{ steps.yarn-lock.outcome == 'success' }}
        # Need to build all dependencies as well to be able to run tests later
        run: yarn lerna -- run build --since origin/master --include-dependencies

      - name: build all packages
        if: ${{ steps.yarn-lock.outcome == 'failure' }}
        run: yarn lerna -- run build

      - name: verify type dependencies
        run: yarn lint:type-deps

      - name: test changed packages
        if: ${{ steps.yarn-lock.outcome == 'success' }}
        run: yarn lerna -- run test --since origin/master -- --coverage

      - name: test all packages
        if: ${{ steps.yarn-lock.outcome == 'failure' }}
        run: yarn lerna -- run test -- --coverage

      - name: verify plugin template
        run: yarn lerna -- run diff -- --check

      - name: bundle example app
        run: yarn bundle

      - name: verify storybook
        run: yarn workspace storybook build-storybook
