name: CI

on:
  push:
    branches: [ master, github-action, develop]
  pull_request:
    branches: [ master ]

jobs:
  build:
    if: "!contains(toJSON(github.event.commits.*.message), '[skip-ci]')"
    name: Test on node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          # - macos-latest
          # - windows-latest
        node_version: [ 10.x, 12.x, 13.x, 14.x, 15.x, 16.x, 18.x, 19.x, 20.x, 21.x, 22.x ]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node_version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node_version }}

    - name: Restore packages
      uses: actions/cache@v2
      with:
        path: |
          node_modules
          */*/node_modules
        key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

    - name: Copy package.json compatible with node version
      run: |
        if [[ ${{ matrix.node_version }} == 10* ]]; then
          cp ci-package.json package.json
        elif [[ ${{ matrix.node_version }} == 12* ]]; then
          cp ci-package.json package.json
        elif [[ ${{ matrix.node_version }} == 13* ]]; then
          cp ci-package.json package.json
        elif [[ ${{ matrix.node_version }} == 14* ]]; then
          cp ci-package.json package.json
        elif [[ ${{ matrix.node_version }} == 15* ]]; then
          cp ci-package.json package.json
        elif [[ ${{ matrix.node_version }} == 16* ]]; then
          cp ci-package.json package.json
        elif [[ ${{ matrix.node_version }} == 19* ]]; then
          cp ci-package.json package.json
        fi

    - name: Install dependencies and run tests with coverage
      run: |
        yarn install
        yarn test:coverage

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v4.0.1
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        flags: ${{ runner.os }}
