name: "🎉 Release new version"

on:
  workflow_dispatch:
    inputs:
      runtime:
        type: choice
        description: JS runtime
        default: "bun"
        options:
          - "bun"
          - "npm"
      release-type:
        type: choice
        description: Release type
        options:
          - " -- --patch"
          - " -- --minor"
  push:
    branches:
      - master
    paths:
      - "src/**"
      - "!src/docs/**"
      - "!src/tests/**"
      - "!src/experiments/**"

jobs:
  build-publish:
    name: 🔨 Build + 🚀 Publish
    timeout-minutes: 10
    runs-on: ubuntu-latest
    env:
      runtime: ${{ inputs.runtime || 'bun'}}
    outputs:
      daisyuiversion: ${{ steps.package-version.outputs.version }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1
          clean: false

      - name: Setup Node
        if: env.runtime == 'npm'
        uses: actions/setup-node@v4
        with:
          node-version: "latest"
          registry-url: https://registry.npmjs.org

      - name: Setup Bun
        if: env.runtime == 'bun'
        uses: oven-sh/setup-bun@v1

      - name: Install package dependencies
        run: ${{ env.runtime }} install

      - name: build package
        run: ${{ env.runtime }} run build

      - name: Run release command
        if: github.repository == 'saadeghi/daisyui'
        run: |
          git config --local user.email "pouya.saadeghi@gmail.com"
          git config --local user.name "Pouya Saadeghi"
          ${{ env.runtime }} run release${{ inputs.release-type }}

      - name: Read package version from package.json
        id: package-version
        run: |
          version=$(grep -o '"version": *"[^"]*"' package.json | cut -d'"' -f4)
          echo version=$version >> $GITHUB_OUTPUT

      - name: Update package version in src/docs/.env and commit
        if: github.repository == 'saadeghi/daisyui'
        id: commit
        run: |
          echo "VITE_DAISYUI_VERSION=${{ steps.package-version.outputs.version }}" > src/docs/.env
          git config --local user.email "pouya.saadeghi@gmail.com"
          git config --local user.name "Pouya Saadeghi"
          git add --all
          if [ -z "$(git status --porcelain)" ]; then
            echo push=false >> $GITHUB_OUTPUT
          else
            git commit -m "Update src/docs/.env" -a
            echo push=true >> $GITHUB_OUTPUT
          fi
        shell: bash

      - name: Push changes
        if: steps.commit.outputs.push == 'true'
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node
        if: env.runtime != 'npm'
        uses: actions/setup-node@v4
        with:
          node-version: "latest"
          registry-url: https://registry.npmjs.org

      - name: Publish package to NPM
        if: github.repository == 'saadeghi/daisyui'
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

  deploy-docs:
    name: 📘 Docs
    needs: build-publish
    uses: ./.github/workflows/deploy-docs.yml
    secrets: inherit
    with:
      daisyuiversion: ${{ needs.build-publish.outputs.daisyuiversion }}

  run-tests:
    name: 🧪 Tests
    needs: build-publish
    uses: ./.github/workflows/run-tests.yml
    secrets: inherit
    with:
      filter: " "

  write-release-notes:
    name: 📝 Release notes
    if: github.repository == 'saadeghi/daisyui'
    needs: build-publish
    uses: ./.github/workflows/write-release-notes.yml
    secrets: inherit
    with:
      daisyuiversion: ${{ needs.build-publish.outputs.daisyuiversion }}
