name: Node CI

on: 
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x, 18.x, 20.x]

    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}

    - name: Setup yarn
      run: |
        corepack --version
        yarn setup
        yarn --version
      
    # Yarn dependencies cannot be cached until yarn is installed
    # WORKAROUND: https://github.com/actions/setup-node/issues/531
    - name: Extract cached dependencies
      uses: actions/setup-node@v3
      with:
        cache: yarn

    - name: Install dependencies
      run: corepack yarn install --immutable
      env:
        CI: true

    - name: lint
      run: yarn lint
      env:
        CI: true

    - name: test
      run: yarn test
      env:
        CI: true