# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Website Check

on:
  workflow_dispatch:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  website_check:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [20.x]

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

      - name: Set up SvelteKit on Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'

      - name: Cache Dependencies
        uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-${{ github.event.pull_request.number }}-

      - name: Install Dependencies
        run: npm ci

      - name: Copy .env from .env.example
        run: cp .env.example .env

      - name: Svelte check
        run: npm run check

      - name: Format
        run: npm run format && git diff --exit-code

      - name: Lint
        run: npm run lint && git diff --exit-code

      - name: Vitest tests
        run: npm t

      # info: https://etok.codes/acmcsuf.com/blob/main/CONTRIBUTING.md#npm-bloat
      - name: Test Dependency Size
        run: npm run test:size

      - name: Transform Genuary Check Changes
        id: transform-genuary-check-changes
        run: |
          if git diff --exit-code --quiet scripts/transform-genuary; then
            echo "updated=0" >> $GITHUB_OUTPUT;
          else
            echo "updated=1" >> $GITHUB_OUTPUT;
          fi

      - name: Test Transform Genuary
        if: steps.transform-genuary-check-changes.outputs.updated == 1
        run: node scripts/transform-genuary/test.js
