name: Build

on: [push]

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-latest,  windows-latest, ubuntu-latest]
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Use Node.js 14.x
        uses: actions/setup-node@v2.1.4
        with:
          node-version: "14.x"
          registry-url: https://registry.npmjs.org

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

      - uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install
        run: yarn  --frozen-lockfile --check-files

      - name: Build
        run: yarn run build

      - name: Lint
        run: yarn run lint

      - name: Check Formatting
        run: git diff --exit-code

      - name: Test
        run: yarn run test

  publish:
    runs-on: ubuntu-latest
    needs: [build]
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
          registry-url: 'https://registry.npmjs.org'
          
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Install
        run: yarn --frozen-lockfile --check-files

      - name: Lint
        run: yarn run lint

      - name: Build
        run: yarn run build

      - name: Check Formatting
        run: git diff --exit-code

      - name: Test
        run: yarn run test

      - name: Publish NPM
        run: npx lerna publish from-git --yes
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}