name: Prepare CLI tool
run-name: "Prepare CLI tool: ${{ inputs.package }}"

on:
  workflow_dispatch:
    inputs:
      package:
        description: '<name>@<version> or <owner>/<repo>#<version>'
        type: string
        required: true
  workflow_call:
    inputs:
      package:
        description: '<name>@<version> or <owner>/<repo>#<version>'
        type: string
        required: true
    outputs:
      artifact:
        description: "Artifact name"
        value: ${{ jobs.pack.outputs.artifact }}
      path:
        description: "Path to package"
        value: ${{ jobs.pack.outputs.path }}
    secrets:
      RETE_QA_PUBLIC_PULL:
        description: 'GitHub Personal access token'
        required: true

jobs:
  pack:
    name: ${{ inputs.package }}
    runs-on: ubuntu-latest
    outputs:
      artifact: ${{ steps.build.outputs.name }}
      path: ${{ steps.result.outputs.path }}

    steps:
    - name: Detect source type
      id: detect-source
      run: |
        regex_github='^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+#.+$'
        regex_npm='^(@[a-zA-Z0-9_-]+\/)?[a-zA-Z0-9_-]+(@[a-zA-Z0-9_.-]+)?$'

        if [[ ${{ inputs.package }} =~ $regex_github ]]; then
          echo "type=github" >> "$GITHUB_OUTPUT"
        elif [[ ${{ inputs.package }} =~ $regex_npm ]]; then
          echo "type=npm" >> "$GITHUB_OUTPUT"
        else
          echo 'Cannot determine source type'
          exit 1
        fi

    - name: Print source type
      run: |
        echo "Source: ${{ steps.detect-source.outputs.type }}"

    - name: Check NPM Package Version
      if: steps.detect-source.outputs.type == 'npm'
      run: |
        npm show ${{ inputs.package }} version || exit

    - name: Extract repo and branch
      if: steps.detect-source.outputs.type == 'github'
      id: repo
      run: |
        echo "path=$(echo ${{ inputs.package }} | cut -d'#' -f1)" >> "$GITHUB_OUTPUT"
        echo "branch=$(echo ${{ inputs.package }} | cut -d'#' -f2)" >> "$GITHUB_OUTPUT"

    - name: Clone from GitHub
      if: steps.detect-source.outputs.type == 'github'
      uses: actions/checkout@v2
      with:
        repository: ${{ steps.repo.outputs.path }}
        ref: ${{ steps.repo.outputs.branch }}
        token: ${{ secrets.RETE_QA_PUBLIC_PULL }}
        fetch-depth: 0

    - name: Setup Node.js
      if: steps.detect-source.outputs.type == 'github'
      uses: actions/setup-node@v4

    - name: Build
      if: steps.detect-source.outputs.type == 'github'
      id: build
      run: |
        npm install
        npm run build
        npm pack
        echo "name=$(node -p "require('./package.json').name")" >> "$GITHUB_OUTPUT"

    - name: Upload *.tgz
      if: steps.detect-source.outputs.type == 'github'
      uses: actions/upload-artifact@v4
      with:
        name: ${{ steps.build.outputs.name }}
        path: ./*.tgz

    - name: Extract tgz path
      if: steps.detect-source.outputs.type == 'github'
      id: tgz
      run: |
        echo "path=$(ls ./*.tgz)" >> "$GITHUB_OUTPUT"

    - name: Print tgz path
      if: steps.detect-source.outputs.type == 'github'
      run: |
        echo "${{ steps.tgz.outputs.path }}"

    - name: Output path
      id: result
      run: |
        echo "path=${{ steps.detect-source.outputs.type == 'github' && steps.tgz.outputs.path || inputs.package }}" >> "$GITHUB_OUTPUT"

