name: 🚀 Deploy - Demo

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  deployment:
    runs-on: ubuntu-latest
    steps:
      - name: ⚙️ Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 8

      - name: ⚙️ Set BRAND_NAME environment variable from repo name
        run: echo BRAND_NAME=${{ github.event.repository.name }} | cut -d '-' -f1 >> $GITHUB_ENV

      - name: ⬇️ Clone current repo under /<brand-name>/nuxt-free
        uses: actions/checkout@v4
        with:
          path: ${{ env.BRAND_NAME }}/nuxt-free

      - name: ⬇️ Clone automation scripts repo under /automation-scripts
        uses: actions/checkout@v4
        with:
          repository: themeselection/automation-scripts
          token: ${{ secrets.GH_PAT }}
          path: automation-scripts

      - name: ⬇️ Install packages in automation-scripts dir
        working-directory: automation-scripts/vue
        run: pnpm install

      - name: ⚙️ Prepare free demo
        working-directory: automation-scripts/vue
        run: pnpm tsx src/templates/${{ env.BRAND_NAME }}/scripts/prepareNuxtFreeDemo.ts

      - name: 📦 Generate demo
        working-directory: ${{ env.BRAND_NAME }}/nuxt-free/typescript-version
        run: pnpm i && pnpm generate
        env:
          NUXT_APP_BASE_URL: /${{ env.BRAND_NAME }}-vuetify-nuxtjs-admin-template-free/demo/

      - name: 🗄️ Ensure Dir on server
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          key: ${{ secrets.SSHKEY }}
          script: |
            mkdir -p ${{ secrets.PROD_DIR }}

      - name: 🚀 Upload demo zip
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          key: ${{ secrets.SSHKEY }}
          source: ${{ env.BRAND_NAME }}/nuxt-free/typescript-version/.output/public
          target: ${{ secrets.PROD_DIR }}
          strip_components: 5
          rm: true
