# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# This workflow will build and test the TypeAgent TypeScript code

name: build-ts

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

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

permissions:
  pull-requests: read
  contents: read
  id-token: write

env:
  NODE_OPTIONS: --max_old_space_size=8192

jobs:
  build_ts:
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest", "windows-latest", "macos-latest"]
        version: [20, 22]

    runs-on: ${{ matrix.os }}
    steps:
      - if: runner.os == 'Linux'
        run: |
          sudo apt install libsecret-1-0
      - name: Setup Git LF
        run: |
          git config --global core.autocrlf false
      - uses: actions/checkout@v4
      - uses: dorny/paths-filter@v3
        id: filter
        continue-on-error: true
        with:
          filters: |
            ts:
              - "ts/**"
              - ".github/workflows/build-ts.yml"
      - uses: pnpm/action-setup@v4
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        name: Install pnpm
        with:
          package_json_file: ts/package.json
      - uses: actions/setup-node@v4
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        with:
          node-version: ${{ matrix.version }}
          cache: "pnpm"
          cache-dependency-path: ts/pnpm-lock.yaml
      - name: Install dependencies
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        working-directory: ts
        run: |
          pnpm install --frozen-lockfile --strict-peer-dependencies
      - name: Build
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        working-directory: ts
        run: |
          npm run build
      - name: Test
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        working-directory: ts
        run: |
          npm run test:local
      - name: Lint
        if: ${{ github.event_name != 'pull_request' || steps.filter.outputs.ts != 'false' }}
        working-directory: ts
        run: |
          npm run lint
