name: Build Documents

on:
  workflow_dispatch:
  push:
    branches:
      - '*'
    paths:
      - "docs/**/*.js"
      - "docs/**/*.ts"
      - "docs/**/*.vue"
      - "docs/package.json"
      - "docs/.env*"
      - "docs/**/*.md"
      - ".github/workflows/doc*.yml"
  pull_request:
    types: [ opened, synchronize, reopened ]
    paths:
      - "docs/**/*.js"
      - "docs/**/*.ts"
      - "docs/**/*.vue"
      - "docs/package.json"
      - "docs/.env*"
      - "docs/**/*.md"
      - ".github/workflows/doc*.yml"
  release:
    types: [published]
  workflow_run:
    workflows: ["Sync branch"]
    types:
      - completed

jobs:
  build:
    runs-on: macos-15
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Set up nodejs
        uses: actions/setup-node@v6
        with:
          node-version: 23.x

      - name: Install dependencies
        run: |
          corepack enable
          corepack prepare pnpm@latest --activate
          pnpm install
        working-directory: docs

      - name: Build
        run: COMMIT_REF=$(git rev-parse HEAD) pnpm docs:build
        working-directory: docs

      - name: Archive artifacts
        uses: actions/upload-artifact@v5
        with:
          name: dist
          path: docs/.vitepress/dist

      - name: Deploy
        if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || github.event_name == 'release' || (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success'))
        uses: cloudflare/wrangler-action@v3
        with:
          apiToken: ${{ secrets.CF_API_TOKEN }}
          accountId: ${{ secrets.CF_ACCOUNT_ID }}
          command: pages deploy "docs/.vitepress/dist/" --commit-dirty=true --project-name=nginxui
