name: Build and Test

on:
  push:
    branches:
      - "*"
  pull_request:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  linting:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Cache NPM modules
        id: npm-cache
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: Install dependencies
        run: npm ci

      - name: Run test suite
        run: npm run lint

  build_components:
    runs-on: ubuntu-latest
    needs: linting
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Cache NPM modules
        id: npm-cache
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: Install dependencies
        run: npm ci

      - name: Build Component
        run: npm run package

      - name: Build Example
        run: npm run build

  publish:
    needs: build_components
    if: github.repository == 'RobBrazier/svelte-awesome' && (github.ref_name == 'master')
    uses: ./.github/workflows/publish.yaml
