name: "Repomix Action"
description: "Pack repository contents into a single file that is easy for LLMs to process"
author: "Kazuki Yamada <koukun0120@gmail.com>"
branding:
  icon: archive
  color: orange

inputs:
  directories:
    description: "Space-separated list of directories to process (defaults to '.')"
    required: false
    default: "."
  include:
    description: "Comma-separated glob patterns to include"
    required: false
    default: ""
  ignore:
    description: "Comma-separated glob patterns to ignore"
    required: false
    default: ""
  output:
    description: "Relative path to write packed file"
    required: false
    default: "repomix-output.xml"
  compress:
    description: "Set to 'false' to disable smart compression"
    required: false
    default: "true"
  style:
    description: "Output style (xml, markdown, plain)"
    required: false
    default: "xml"
  additional-args:
    description: "Any extra raw arguments to pass directly to the repomix CLI"
    required: false
    default: ""
  repomix-version:
    description: "Version (or tag) of the npm package to install – defaults to latest"
    required: false
    default: "latest"
  node-version:
    description: "Node.js version to use (defaults to 24)"
    required: false
    default: "24"

runs:
  using: "composite"
  steps:
    - name: Setup Node.js
      uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
      with:
        node-version: ${{ inputs.node-version }}
    - name: Install project dependencies
      shell: bash
      run: |
        # Install project dependencies if package.json exists
        # This ensures repomix.config.ts can import from local source
        if [ -f "package.json" ]; then
          npm install
        fi
    - name: Install Repomix
      shell: bash
      env:
        REPOMIX_VERSION: ${{ inputs.repomix-version }}
      run: |
        npm install --global "repomix@${REPOMIX_VERSION}"
    - name: Run Repomix
      id: build
      shell: bash
      env:
        INPUT_DIRECTORIES: ${{ inputs.directories }}
        INPUT_INCLUDE: ${{ inputs.include }}
        INPUT_IGNORE: ${{ inputs.ignore }}
        INPUT_COMPRESS: ${{ inputs.compress }}
        INPUT_STYLE: ${{ inputs.style }}
        INPUT_OUTPUT: ${{ inputs.output }}
        INPUT_ADDITIONAL_ARGS: ${{ inputs.additional-args }}
      run: |
        set -e
        # Using an array for safer command execution
        # Safely split directories input into an array, handling spaces correctly
        IFS=' ' read -r -a ARGS <<< "${INPUT_DIRECTORIES}"

        if [ -n "${INPUT_INCLUDE}" ]; then
          ARGS+=(--include "${INPUT_INCLUDE}")
        fi

        if [ -n "${INPUT_IGNORE}" ]; then
          ARGS+=(--ignore "${INPUT_IGNORE}")
        fi

        if [ "${INPUT_COMPRESS}" = "true" ]; then
          ARGS+=(--compress)
        fi

        if [ -n "${INPUT_STYLE}" ]; then
          ARGS+=(--style "${INPUT_STYLE}")
        fi

        ARGS+=(--output "${INPUT_OUTPUT}")

        # Only add additional args if not empty
        if [ -n "${INPUT_ADDITIONAL_ARGS}" ]; then
          # Use safer parsing for additional arguments
          IFS=' ' read -r -a ADDITIONAL_ARGS <<< "${INPUT_ADDITIONAL_ARGS}"
          ARGS+=("${ADDITIONAL_ARGS[@]}")
        fi

        echo "Running: repomix ${ARGS[*]}"
        repomix "${ARGS[@]}"

        echo "output_file=${INPUT_OUTPUT}" >> "$GITHUB_OUTPUT"

outputs:
  output_file:
    description: "Path to the file generated by Repomix"
    value: ${{ steps.build.outputs.output_file }}
