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

trigger:
  branches:
    include:
      - main

pr:
  branches:
    exclude:
      - "*"

pool:
  vmImage: "ubuntu-latest"

variables:
  - name: buildDirectory
    value: $(Build.SourcesDirectory)/ts
  - name: agentsdk_folder
    value: $(buildDirectory)/packages/agentSdk

jobs:
  - job: build_ts
    displayName: "Build TypeScript Project"
    strategy:
      matrix:
        node_22:
          nodeVersion: "22"
    steps:
      - template: include-prepare-repo.yml
        parameters:
          buildDirectory: $(buildDirectory)
          nodeVersion: $(nodeVersion)
          registry: $(REGISTRY)

      - template: include-update-package-version.yml
        parameters:
          packageFolder: $(agentsdk_folder)
          prerelease: $(Build.BuildId)

      - script: |
          npm run build
        displayName: "Build"
        workingDirectory: $(buildDirectory)

      - script: |
          sudo apt install libsecret-1-0
        displayName: "Install libsecret-1-0"
        condition: and(succeeded(), eq(variables['Agent.OS'], 'Linux'))

      - script: |
          npm run test:local
        displayName: "Run Tests (Local)"
        workingDirectory: $(buildDirectory)

      - script: |
          npm run lint
        displayName: "Lint"
        workingDirectory: $(buildDirectory)

      - script: |
          echo $(ADO_REGISTRY)
          echo "registry=$(ADO_REGISTRY)" > .npmrc
          echo "always-auth=true" >> .npmrc
          cat .npmrc
        displayName: "Create .npmrc file."
        workingDirectory: $(agentsdk_folder)

      - task: npmAuthenticate@0
        inputs:
          workingFile: "$(agentsdk_folder)/.npmrc"
        displayName: "Authenticate with Azure Artifacts"

      - script: |
          npm publish --registry=$(ADO_REGISTRY)
        displayName: "Pack and Publish agent-sdk Module"
        workingDirectory: $(agentsdk_folder)
