name: CI

on:
  push:
    branches:
      - dev
      - master
  pull_request:
    branches:
      - dev
      - master

env:
  NODE_OPTIONS: --max_old_space_size=6144
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    name: Lint and check format
    runs-on: ubuntu-latest
    steps:
      - name: Check out files from GitHub
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      - name: Setup Node
        uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
        with:
          node-version-file: ".nvmrc"
          cache: yarn
      - name: Install dependencies
        run: yarn install --immutable
      - name: Check for duplicate dependencies
        run: yarn dedupe --check
      - name: Build resources
        run: ./node_modules/.bin/gulp gen-icons-json build-translations build-locale-data gather-gallery-pages
      - name: Setup lint cache
        uses: actions/cache@0400d5f644dc74513175e3cd8d07132dd4860809 # v4.2.4
        with:
          path: |
            node_modules/.cache/prettier
            node_modules/.cache/eslint
            node_modules/.cache/typescript
          key: lint-${{ github.sha }}
          restore-keys: lint-
      - name: Run eslint
        run: yarn run lint:eslint --quiet
      - name: Run tsc
        run: yarn run lint:types
      - name: Run lit-analyzer
        run: yarn run lint:lit --quiet
      - name: Run prettier
        run: yarn run lint:prettier
  test:
    name: Run tests
    runs-on: ubuntu-latest
    steps:
      - name: Check out files from GitHub
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      - name: Setup Node
        uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
        with:
          node-version-file: ".nvmrc"
          cache: yarn
      - name: Install dependencies
        run: yarn install --immutable
      - name: Build resources
        run: ./node_modules/.bin/gulp gen-icons-json build-translations build-locale-data
      - name: Run Tests
        run: yarn run test
  build:
    name: Build frontend
    needs: [lint, test]
    runs-on: ubuntu-latest
    steps:
      - name: Check out files from GitHub
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      - name: Setup Node
        uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
        with:
          node-version-file: ".nvmrc"
          cache: yarn
      - name: Install dependencies
        run: yarn install --immutable
      - name: Build Application
        run: ./node_modules/.bin/gulp build-app
        env:
          IS_TEST: "true"
      - name: Upload bundle stats
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: frontend-bundle-stats
          path: build/stats/*.json
          if-no-files-found: error
  supervisor:
    name: Build supervisor
    needs: [lint, test]
    runs-on: ubuntu-latest
    steps:
      - name: Check out files from GitHub
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
      - name: Setup Node
        uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
        with:
          node-version-file: ".nvmrc"
          cache: yarn
      - name: Install dependencies
        run: yarn install --immutable
      - name: Build Application
        run: ./node_modules/.bin/gulp build-hassio
        env:
          IS_TEST: "true"
      - name: Upload bundle stats
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: supervisor-bundle-stats
          path: build/stats/*.json
          if-no-files-found: error
