name: Appium Build
on:
  push:
    branches:
      - master
    # this list of paths could potentially impact the build
    paths:
      - '**/tsconfig*.json'
      - 'packages/base-driver/static/**/*'
      - 'packages/*/test/**'
      - 'packages/*/bin'
      - 'packages/**/*.ts'
      - 'packages/**/*.json'
      - 'packages/**/*.js'
      - 'package*.json'
      - 'lerna.json'
      - '.npmrc'
      - '.mocharc.js'
      - '.github/workflows/build.yml' # this file
      - '.eslintrc'
      - '.eslintignore'
      - '!**/sample-code/**'
      - '!packages/*/docs/**'
  pull_request:
    branches:
      - master
    paths:
      - '**/tsconfig*.json'
      - 'packages/base-driver/static/**/*'
      - 'packages/*/test/**'
      - 'packages/*/bin'
      - 'packages/**/*.ts'
      - 'packages/**/*.json'
      - 'packages/**/*.js'
      - 'package*.json'
      - 'lerna.json'
      - '.npmrc'
      - '.mocharc.js'
      - '.github/workflows/build.yml' # this file
      - '.eslintrc'
      - '.eslintignore'
      - '!**/sample-code/**'
      - '!packages/*/docs/**'

env:
  CI: true

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  prepare_matrix:
    runs-on: ubuntu-latest
    outputs:
      versions: ${{ steps.generate-matrix.outputs.active }}
    steps:
    - name: Select all active LTS versions of Node.js
      id: generate-matrix
      uses: msimerson/node-lts-versions@v1

  test:
    needs:
    - prepare_matrix
    name: Tests
    strategy:
      matrix:
        node-version: ${{ fromJSON(needs.prepare_matrix.outputs.versions) }}
        os:
        - ubuntu-latest
        # TODO: Windows is not stable and slow to run
        # - windows-latest
        # TODO: Enable below envs after all tests have been verified green
        # - macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - name: Install dependencies
        uses: bahmutov/npm-install@v1
        with:
          useRollingCache: true
          install-command: npm ci --foreground-scripts
      - name: Run smoke, unit & E2E tests
        run: npm run test:ci

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install dependencies
        uses: bahmutov/npm-install@v1
        with:
          useRollingCache: true
          install-command: npm ci
      - name: ESLint
        run: npm run lint:ci
