name: Lint & Test

on:
  push:
    tags: [ 'v*' ]
  release:
    types: [released]
  # Triggers the workflow only when merging pull request to the branches.
  pull_request:
    types: [closed]
    branches: [ master, 'Release-*' ]
  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  prepare:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 16.x

      - name: Cache node_modules
        uses: actions/cache@v2
        id: cache-npm-packages
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}

      - name: Install NPM dependencies
        if: steps.cache-npm-packages.outputs.cache-hit != 'true'
        run: npm ci

  lint:
    name: Lint
    runs-on: ubuntu-latest
    needs: prepare
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 16.x

      - name: Cache node_modules
        uses: actions/cache@v2
        id: cache-npm-packages
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}

      - name: Install NPM dependencies
        if: steps.cache-npm-packages.outputs.cache-hit != 'true'
        run: npm ci

      - name: Lint Scripts
        run: npm run lint

      - name: Lint Server Script
        run: npm run lintServer

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: prepare
    env:
      CI: true
    steps:
      - name: Checkout source code
        uses: actions/checkout@v2

      - name: Setup Node
        uses: actions/setup-node@v2
        with:
          node-version: 16.x

      - name: Cache node_modules
        uses: actions/cache@v2
        id: cache-npm-packages
        with:
          path: node_modules
          key: ${{ runner.OS }}-build-${{ hashFiles('**/package-lock.json') }}

      - name: Install NPM dependencies
        if: steps.cache-npm-packages.outputs.cache-hit != 'true'
        run: npm ci

      - name: Run tests
        run: npm run test
