name: Deploy git-hash-art-www

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      version:
        description: 'Package version to deploy (e.g. 0.10.0)'
        required: true

jobs:
  bump-www:
    runs-on: ubuntu-latest
    steps:
      - name: Extract version
        id: version
        run: |
          if [ -n "${{ github.event.inputs.version }}" ]; then
            VERSION="${{ github.event.inputs.version }}"
          else
            VERSION="${GITHUB_REF_NAME#v}"
          fi
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"

      - name: Clone git-hash-art-www
        uses: actions/checkout@v4
        with:
          repository: gfargo/v0-git-hash-art-www
          token: ${{ secrets.WWW_DEPLOY_PAT }}
          path: www

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 9
          run_install: false

      - name: Update git-hash-art dependency
        working-directory: www
        run: |
          pnpm install git-hash-art@${{ steps.version.outputs.version }}

      - name: Commit and push
        working-directory: www
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add package.json pnpm-lock.yaml
          git diff --cached --quiet && echo "No changes to commit" && exit 0
          git commit -m "chore: bump git-hash-art to v${{ steps.version.outputs.version }}"
          git push
