name: "Turborepo Node.js Setup"
description: "Sets Node.js up for CI"
inputs:
  enable-corepack:
    description: "Control turning on corepack."
    required: false
    default: "true"
  extra-flags:
    description: "Extra flags to pass to the pnpm install."
    required: false
    default: ""
  package-install:
    description: "Don't run the install step."
    required: false
    default: "true"
  node-version:
    description: "Node version to install"
    required: false

runs:
  using: "composite"
  steps:
    - name: Setup pnpm
      uses: pnpm/action-setup@v4

    - name: Setup Node.js
      uses: actions/setup-node@v4
      with:
        # node-version-file is the default, but can be overridden using node-version
        node-version-file: "package.json"
        node-version: ${{ inputs.node-version }}
        cache: pnpm

    - name: Upgrade corepack
      if: ${{ inputs.enable-corepack == 'true' }}
      shell: bash
      # Forcibly upgrade our available version of corepack.
      # The bundled version in node 16 has known issues.
      # Prepends the npm bin dir so that it is always first.
      run: |
        npm install --force --global corepack@latest
        npm config get prefix >> $GITHUB_PATH

    - name: Configure corepack
      if: ${{ inputs.enable-corepack == 'true' }}
      shell: bash
      run: corepack enable

    - name: pnpm install
      id: install
      if: ${{ inputs.package-install == 'true' }}
      continue-on-error: true
      shell: bash
      run: pnpm install ${{ inputs.extra-flags }}

    - name: pnpm install (second try)
      if: ${{ steps.install.outcome == 'failure' }}
      shell: bash
      run: pnpm install ${{ inputs.extra-flags }}
