name: CI

on: 
  push:
    branches:
      - '**'

  workflow_dispatch:

  workflow_call:

jobs:
  test_and_build:
    runs-on: ubuntu-latest

    steps:
        # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up node.js
        uses: actions/setup-node@v3.3.0
        with:
          node-version-file: .nvmrc

      - name: Cache node modules
        id: cache-nodemodules
        uses: actions/cache@v3
        env:
          cache-name: cache-node-modules
        with:
          path: node_modules
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

      - name: Install dependencies
        if: steps.cache-nodemodules.outputs.cache-hit != 'true'
        run: npm ci

      - name: Test
        run: npm test

      - name: Build and prepare for a potential 'npm publish'
        run: gulp npm-prepare

      - name: Package artifact for upload
        run: tar -czvf artifact.tar.gz dist
        shell: bash

      - uses: actions/upload-artifact@v3
        with:
          path: |
            ${{ github.workspace }}/artifact.tar.gz
          if-no-files-found: error
          retention-days: 1

