
name: Build, Deploy Docs, Release

on:
  push:
    branches: ["main"]
    paths:
      - "src/**"
      - ".jsdoc/**"
      - "README.md"

  workflow_dispatch: # Allow run from Actions tab

permissions:
  contents: write
  pages: write
  id-token: write

concurrency:
  group: "deployment"
  cancel-in-progress: false

jobs:
  build:
    name: Build, Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout main
        uses: actions/checkout@v3
        with:
          ref: ${{ github.ref }}

      # Quick n dirty custom minify "action", because the actions available on the marketplace aren't quite what I need. Maybe I ought to roll my own minify action?
      - name: Minify Source
        run: |
          rm -rf dist
          mkdir -p dist
          cp -r src/* dist/
          npm i uglify-js@3.17.4 --save-dev
          for f in `find dist -name "*.js" -type f`; do
            basename=$(basename $f)
            extension="${basename##*.}"
            filename="${basename%.*}"
            output="${f%/*}/"
            output_path="${output}${filename}.min.${extension}"
            if [ "${output_path}" != "${f}" ]; then
              rm -rf "${output_path}"
            fi
            echo "uglifying ${f} > ${output_path}"
            sed -i -E 's/^(import.*from "\.{1,2}\/.*)\.js/\1\.min\.js/g' $f
            npx uglifyjs -c -m --mangle-props regex="/^#/" --module --source-map "url='${basename}.map'" -o "${output_path}" -- "${f}"
          done

      - name: Generate Docs
        uses: andstor/jsdoc-action@v1
        with:
          source_dir: ./src
          recurse: true
          output_dir: ./docs
          front_page: ./README.md
          template: clean-jsdoc-theme
          config_file: ./.jsdoc/conf.json

      - name: Push Changes
        run: |
          git config --local user.name github-actions[bot]
          git config --local user.email github-actions[bot]@github.com
          git add -f dist/*
          git add -f docs/*
          git commit -m "ci: Minify, generate docs"
          git push

      - name: Setup Pages
        uses: actions/configure-pages@v3
      - name: Build with Jekyll
        uses: actions/jekyll-build-pages@v1
        with:
          source: ./docs
          destination: ./_site
      - name: Upload Pages artifact
        uses: actions/upload-pages-artifact@v2

      - name: Create Release
        id: release
        uses: dev-build-deploy/release-me@v0
        with:
          token: ${{ github.token }}
          versioning: semver
          config: .github/release.yml

      - name: Determine URLs to Purge
        id: purge_urls
        if: steps.release.outputs.created
        env:
          VER: ${{ fromJSON(steps.release.outputs.release).tag_name }}
        run: |
          baseUrl="https://cdn.jsdelivr.net/gh/${{ github.repository }}@"
          major="${VER%%.*}"
          minor="${VER%.*}"
          urls=""
          for f in `git diff ${{ github.sha }} --name-only dist/**.js`; do
            urls+="${baseUrl}${major}/${f},"
            urls+="${baseUrl}${minor}/${f},"
            urls+="${baseUrl}latest/${f},"
          done
          echo "${urls}"
          echo "urls=${urls%,*}" >> $GITHUB_OUTPUT

      - name: Purge jsDelivr Cache
        if: success() && steps.release.outputs.created
        uses: egad13/purge-jsdelivr-cache@v1
        with:
          url: ${{ steps.purge_urls.outputs.urls }}
          attempts: 1

  deploy-docs:
    name: Deploy Docs to GitHub Pages
    needs: build
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - id: deployment
        uses: actions/deploy-pages@v2
