# This is the composite action:
#   https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
#
# Composite actions have some limitations:
#   - many contexts are unavailable, e.g. `runner`
#   - `env` can be specified per-step only
#   - if `run` is used, `shell` must be explicitly specified
name: 'Setup Node and install dependencies'
description: 'Setup Node and install dependencies using cache'
inputs:
  node-version:
    description: 'Node version'
    required: true
  os:
    description: 'Composite actions do not support `runner.os`, so it must be passed in as an input'
    required: true
  save-cache:
    description: 'Save cache when needed'
    required: false
    default: 'false'
runs:
  using: 'composite'
  steps:
    - name: Calculate `CACHE_KEY`
      shell: bash
      run: |
        echo 'CACHE_KEY=node_modules-${{
          inputs.os
        }}-${{
          inputs.node-version
        }}-${{
          hashFiles('pnpm-lock.yaml', 'package.json')
        }}' >> "$GITHUB_ENV"

    - name: Restore `node_modules`
      id: node-modules-restore
      uses: actions/cache/restore@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
      with:
        path: node_modules
        key: ${{ env.CACHE_KEY }}
        enableCrossOsArchive: true

    - name: Calculate `CACHE_HIT`
      shell: bash
      run: |
        echo 'CACHE_HIT=${{
          (steps.node-modules-restore.outputs.cache-hit == 'true') && 'true' || ''
        }}' >> "$GITHUB_ENV"

    - name: Setup pnpm
      uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
      with:
        standalone: true

    - name: Setup Node
      uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
      with:
        node-version: ${{ inputs.node-version }}

    - name: Calculate `PNPM_STORE`
      shell: bash
      run: |
        echo "PNPM_STORE=$(pnpm store path)" >> "$GITHUB_ENV"

    - name: Cache and restore `pnpm store`
      if: env.CACHE_HIT != 'true'
      uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
      with:
        path: ${{ env.PNPM_STORE }}
        key: |
          pnpm_store-${{
          inputs.os
          }}-${{
            inputs.node-version
          }}-${{
            hashFiles('pnpm-lock.yaml', 'package.json')
          }}
        enableCrossOsArchive: true

    - name: Install dependencies
      uses: nick-fields/retry@ce71cc2ab81d554ebbe88c79ab5975992d79ba08 # v3.0.2
      if: env.CACHE_HIT != 'true'
      with:
        timeout_minutes: 10
        max_attempts: 3
        command: pnpm install --frozen-lockfile

    - name: Write `node_modules` cache
      if: inputs.save-cache == 'true' && env.CACHE_HIT != 'true'
      uses: actions/cache/save@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
      with:
        path: node_modules
        key: ${{ env.CACHE_KEY }}
        enableCrossOsArchive: true

    - name: Generate files
      shell: bash
      run: >
        if [[ -d lib ]]; then
          pnpm prepare:generate;
        fi

    - name: Git config
      shell: bash
      run: |
        git config --global core.autocrlf false
        git config --global core.symlinks true
        git config --global user.email 'renovate@whitesourcesoftware.com'
        git config --global user.name  'Renovate Bot'
