name: CI
on:
  release:
    types: [released]
  push:
    branches:
      - main
  pull_request:
  schedule:
    - cron: '0 0 * * *'
permissions:
  contents: read
env:
  NODE_VERSION: 14
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup NodeJS
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'
          cache-dependency-path: package-lock.json

      - name: Patch NPM version # Lockfile v3 is not compatible with default npm@6 installed with NodeJS 14
        run: npm install -g npm@9.9.4

      - name: Install deps
        run: npm ci --legacy-peer-deps

      - name: Build
        run: npm run build

      - name: Package
        run: npm pack

      - uses: actions/upload-artifact@v7
        with:
          name: quickcase-angular-case-ui-toolkit.tgz
          path: quickcase-angular-case-ui-toolkit-*.tgz
          retention-days: 7

  quality_checks:
    name: Quality checks
    runs-on: ubuntu-latest
    timeout-minutes: 5
    strategy:
      matrix:
        node: [14]
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup NodeJS ${{ matrix.node }}
        uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node }}
          cache: 'npm'
          cache-dependency-path: package-lock.json

      - name: Patch NPM version # Lockfile v3 is not compatible with default npm@6 installed with NodeJS 14
        run: npm install -g npm@9.9.4

      - name: Install deps
        run: npm ci --legacy-peer-deps

      - name: Run tests
        run: npm test
      - name: Check coverage
        run: npm run test:coverage

  lint:
    name: Lint
    if: github.ref != 'refs/heads/main' # Don't run for main branch
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup NodeJS
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'
          cache-dependency-path: package-lock.json

      - name: Patch NPM version # Lockfile v3 is not compatible with default npm@6 installed with NodeJS 14
        run: npm install -g npm@9.9.4

      - name: Install deps
        run: npm ci --legacy-peer-deps

      - name: Lint
        run: npm run lint

  publish:
    name: Publish
    needs: [build, quality_checks, lint]
    if: github.event_name == 'release' && github.event.action == 'released'
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Setup NodeJS
        uses: actions/setup-node@v6
        with:
          node-version: ${{ env.NODE_VERSION }}
          cache: 'npm'
          cache-dependency-path: package-lock.json
          registry-url: https://registry.npmjs.org
          scope: '@quickcase'

      - name: Patch NPM version # Lockfile v3 is not compatible with default npm@6 installed with NodeJS 14
        run: npm install -g npm@9.9.4

      - name: Install deps
        run: npm ci --legacy-peer-deps

      - name: Publish
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
