name: Cache Yarn

description: Cache Yarn dependencies with consistent keys and paths.

inputs:
  path:
    description: Paths to cache
    required: false
    default: |
      .yarn/cache
  lock-file:
    description: Path to yarn.lock
    required: false
    default: yarn.lock
  cache-version:
    description: Additional cache version segment
    required: false
    default: v1

outputs:
  cache-hit:
    description: Whether an exact match was found for the cache key
    value: ${{ steps.cache.outputs.cache-hit }}

runs:
  using: "composite"
  steps:
    - id: get-hash
      name: Hash lock file
      shell: bash
      run: |
        if [ -f "${{ inputs.lock-file }}" ]; then
          echo "hash=$(shasum -a 256 "${{ inputs.lock-file }}" | awk '{ print $1 }')" >> $GITHUB_OUTPUT
        else
          echo "::warning::Lock file '${{ inputs.lock-file }}' not found."
          echo "hash=no-lock-file" >> $GITHUB_OUTPUT
        fi
    - id: cache
      name: Cache Yarn dependencies
      uses: actions/cache@v4
      with:
        path: ${{ inputs.path }}
        key: ${{ runner.os }}-yarn-${{ inputs.cache-version }}-${{ steps.get-hash.outputs.hash }}
        restore-keys: |
          ${{ runner.os }}-yarn-${{ inputs.cache-version }}-
          ${{ runner.os }}-yarn-
