name: "Setup and build the repo"
description: "A task to reuse setup steps during multiple jobs"
inputs:
  node:
    description: Node version
    required: true

runs:
  using: "composite"
  steps:
    - name: Setup Node
      uses: actions/setup-node@v4
      with:
        node-version: ${{inputs.node}}
        check-latest: true
        # The hash of yarn.lock file will be used as cache key
        # So unless our dependencies change, we can reuse the cache
        cache: yarn

    - name: Node.js version
      id: node
      shell: bash
      run: echo "v8CppApiVersion=$(node --print "process.versions.modules")" >> $GITHUB_OUTPUT

    - name: Create cache key
      id: build-cache
      shell: bash
      # This build cache will be reused among different jobs for the same commit
      run: echo "key=build-cache-${{ runner.os }}-${{ runner.arch }}-node-${{ inputs.node }}-${{ github.sha }}" >> $GITHUB_OUTPUT

    - name: Restore build
      uses: actions/cache/restore@v4
      id: cache-build-restore
      with:
        path: |
          lib/
          packages/*/lib
          packages/*/.git-data.json
        key: ${{ steps.build-cache.outputs.key }}

    - name: Install & build
      shell: bash
      run: yarn install --frozen-lockfile && yarn build

    - name: Check Build
      shell: bash
      run: yarn check-build

    - name: Build bundle
      shell: bash
      run: yarn build:bundle

    - name: Check bundle
      shell: bash
      run: yarn check-bundle

    - name: Cache build artifacts
      uses: actions/cache@v4
      with:
        path: |
          lib/
          packages/*/lib
          packages/*/.git-data.json
        key: ${{ steps.build-cache.outputs.key }}
