# Note: This is a composite GitHub Action.
# Docs: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action

name: setup
description: Hive CI environment setup

inputs:
  codegen:
    description: Should run GraphQL Codegen?
    default: 'true'
  installDependencies:
    description: Should run pnpm install?
    default: 'true'
  workingDirectory:
    description: Working dir
    default: ${{ github.workspace }}
  actor:
    description: Name of the workflow that called this action
    required: true
    type: string

runs:
  using: 'composite'
  steps:
    - name: check pnpm version
      shell: bash
      id: pnpm
      env:
        INSTALL_DEPENDENCIES: ${{ inputs.installDependencies }}
      run: |
        PNPM_VERSION=$(cat package.json | jq -r '.packageManager' | awk -F@ '{print $2}')
        PNPM_VERSION=${PNPM_VERSION:-9}
        echo "Using PNPM version $PNPM_VERSION"
        echo "version=$PNPM_VERSION" >> $GITHUB_OUTPUT

        if [ $INSTALL_DEPENDENCIES = true ]; then
          echo "cache=pnpm" >> $GITHUB_OUTPUT
        else
          echo "cache=" >> $GITHUB_OUTPUT
        fi

    - name: install pnpm
      uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0
      with:
        version: ${{ steps.pnpm.outputs.version }}

    - name: install nodejs
      uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
      with:
        node-version-file: .node-version
        cache: ${{ steps.pnpm.outputs.cache }}

    - name: pnpm install
      shell: bash
      if: ${{ inputs.installDependencies == 'true' }}
      working-directory: ${{ inputs.workingDirectory }}
      run: pnpm install --frozen-lockfile

    - name: build laboratory
      shell: bash
      if: ${{ inputs.buildLaboratory == 'true' }}
      working-directory: ${{ inputs.workingDirectory }}
      run: pnpm turbo build --filter=./packages/libraries/laboratory --color

    - name: generate graphql types
      if: ${{ inputs.codegen == 'true' }}
      shell: bash
      working-directory: ${{ inputs.workingDirectory }}
      run: pnpm graphql:generate
