name: CI

on:
  push:
    branches:
      - main

  pull_request:
    branches:
      - main

# cancel in-progress runs on new commits to same PR (gitub.event.number)
concurrency:
  group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
  cancel-in-progress: true

env:
  NODE_OPTIONS: --max_old_space_size=4096

jobs:
  build:
    name: 🏗️  Build
    runs-on: ubuntu-latest
    timeout-minutes: 15
    outputs:
      published: ${{ steps.changesets.outputs.published }}
      hasChangesets: ${{ steps.changesets.outputs.hasChangesets }}
    permissions:
      contents: write # to create release (changesets/action)
      pull-requests: write # to create pull request (changesets/action)

    steps:
      - name: 📥  Checkout
        uses: actions/checkout@v3
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: 🔧  Setup
        uses: ./.github/actions/setup

      - name: 🩺  Check
        run: pnpm check

      - name: 🤘  Test
        run: pnpm test:coverage

      - name: 📖  Coveralls
        continue-on-error: true
        # prevents this step from running on forks
        if: ${{ github.repository == 'tw-in-js/twind' }}
        # until https://github.com/coverallsapp/github-action/issues/135 is closed
        uses: chenrui333/github-action@a1a1a8a300a7e89df3630639df8fb23de5cc6368
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          path-to-lcov: ./coverage/lcov.info

      - name: 🚚  Create Release PR or Publish to npm
        id: changesets
        if: ${{ github.repository == 'tw-in-js/twind' && github.event_name == 'push' }}
        uses: changesets/action@v1
        with:
          version: pnpm changeset:version
          publish: pnpm changeset:publish
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      # wait a bit and then set latest for twind back to v0.16
      # because some are using CDNs without specifing a version
      - name: 📝  Keep latest tag on twind@v0.16.18
        if: steps.changesets.outputs.published == 'true'
        run: |
          sleep 20
          cat << EOF > "$HOME/.npmrc"
            //registry.npmjs.org/:_authToken=$NPM_TOKEN
          EOF
          npm dist-tag add twind@0.16.18 latest || true
          npm deprecate twind@1.x "use @twind/core instead" || true
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: 🌎  Deploy twind.style
        if: ${{ steps.changesets.outputs.published == 'true' && github.repository == 'tw-in-js/twind' }}
        uses: ./.github/actions/publish-pages
        with:
          workingDirectory: sites/twind.style
          directory: build
          CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
          CLOUDLFARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          deploymentName: twind.style
          projectName: twind-style
          branch: main

      - name: 🌎  Deploy twind.run
        if: ${{ steps.changesets.outputs.published == 'true' && github.repository == 'tw-in-js/twind' }}
        uses: ./.github/actions/publish-pages
        with:
          workingDirectory: sites/twind.run
          directory: .svelte-kit/cloudflare
          CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
          CLOUDLFARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          deploymentName: twind.run
          projectName: twind-run
          branch: main

  publish-pre-releases:
    name: 🚚  Publish pre-releases
    needs: build
    runs-on: ubuntu-latest
    timeout-minutes: 15
    if: ${{ needs.build.outputs.published != 'true' && github.repository == 'tw-in-js/twind' }}
    permissions:
      contents: read
      pull-requests: write
      deployments: write

    steps:
      - name: 📥  Checkout
        uses: actions/checkout@v3

      - name: 🔧  Setup
        uses: ./.github/actions/setup
        with:
          # only install dependencies for packages that are published and for the sites
          installArgs: --filter="." --filter="./packages/*" --filter="./sites/*"

      - name: 🚚  Publish pre-release to npm
        id: publish
        uses: ./.github/actions/publish-prerelease
        with:
          tag: ${{ (github.event_name == 'push' && 'next') || 'canary'}}
          template: ${{ (github.event_name == 'push' && '{tag}-{datetime}') || '{tag}-{commit}'}}
          hasChangesets: ${{ needs.build.outputs.hasChangesets }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      # deploy sites using the published package versions
      - name: 🌎  Deploy twind.style
        id: deploy_twind_style
        uses: ./.github/actions/publish-pages
        with:
          workingDirectory: sites/twind.style
          directory: build
          CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
          CLOUDLFARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
          # no github deployment for canary
          GITHUB_TOKEN: ${{ (github.event.number && '') || secrets.GITHUB_TOKEN }}
          deploymentName: twind.style
          projectName: twind-style
          branch: ${{ (github.event.number && format('pr-{0}', github.event.number)) || (github.event_name == 'push' && 'next') || 'canary' }}

      - name: 🌎  Deploy twind.run
        id: deploy_twind_run
        uses: ./.github/actions/publish-pages
        with:
          workingDirectory: sites/twind.run
          directory: .svelte-kit/cloudflare
          CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
          CLOUDLFARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
          # no github deployment for canary
          GITHUB_TOKEN: ${{ (github.event.number && '') || secrets.GITHUB_TOKEN }}
          deploymentName: twind.run
          projectName: twind-run
          branch: ${{ (github.event.number && format('pr-{0}', github.event.number)) || (github.event_name == 'push' && 'next') || 'canary' }}

      - name: 📝  Comment PR
        if: ${{ github.event.number }}
        uses: thollander/actions-comment-pull-request@v1
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          comment_includes: '## Deployed site previews with [![Cloudflare Pages]'
          message: |
            > **Latest commit**: ${{ github.sha }}

            ## Deployed site previews with [![Cloudflare Pages](https://img.shields.io/badge/Cloudflare%20Pages-F38020?style=for-the-badge&logo=Cloudflare&logoColor=white)](https://pages.dev)

            ### twind.style

            <table>
            <tr><td><strong>Deployment URL:</strong></td><td><a href='${{ steps.deploy_twind_style.outputs.url }}'>${{ steps.deploy_twind_style.outputs.url }}</a></td></tr>
            <tr><td><strong>Branch URL:</strong></td><td><a href='${{ steps.deploy_twind_style.outputs.branch }}'>${{ steps.deploy_twind_style.outputs.branch }}</a></td></tr>
            <tr><td><strong>Version URL:</strong></td><td><a href='${{ steps.deploy_twind_style.outputs.version }}'>${{ steps.deploy_twind_style.outputs.version }}</a></td></tr>
            </table>

            ### twind.run

            <table>
            <tr><td><strong>Deployment URL:</strong></td><td><a href='${{ steps.deploy_twind_run.outputs.url }}'>${{ steps.deploy_twind_run.outputs.url }}</a></td></tr>
            <tr><td><strong>Branch URL:</strong></td><td><a href='${{ steps.deploy_twind_run.outputs.branch }}'>${{ steps.deploy_twind_run.outputs.branch }}</a></td></tr>
            <tr><td><strong>Version URL:</strong></td><td><a href='${{ steps.deploy_twind_run.outputs.version }}'>${{ steps.deploy_twind_run.outputs.version }}</a></td></tr>
            </table>

            ## Published to [npm](https://www.npmjs.com)

            **Dist Tag**: ${{ steps.publish.output.tag }}

            **Packages**:

            ${{ steps.publish.output.packages }}
