# Node.js
# Build a general Node.js application with npm.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/vsts/pipelines/languages/javascript

jobs:
- job: Linux
  pool:
    vmImage: 'ubuntu-18.04'
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'
  - task: YarnInstaller@3
    inputs:
      versionSpec: '1.x'
    displayName: 'Install Yarn'
  - task: CacheBeta@1
    inputs:
      key: yarn2 | $(Agent.OS) | yarn.lock
      path: node_modules
    displayName: Cache node modules
  - script: yarn --frozen-lockfile
    displayName: 'Install dependencies and build'
  - script: |
      yarn test-unit-coverage --forbid-only
      EXIT_CODE=$?
      ./node_modules/.bin/nyc report --reporter=cobertura
      exit $EXIT_CODE
    displayName: 'Unit tests'
  - script: yarn lint
    displayName: 'Lint'
  - task: PublishCodeCoverageResults@1
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/*coverage.xml'
    displayName: 'Publish coverage'

- job: macOS
  pool:
    vmImage: 'macOS-10.15'
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'
  - task: CacheBeta@1
    inputs:
      key: yarn2 | $(Agent.OS) | yarn.lock
      path: node_modules
    displayName: Cache node modules
  - script: yarn --frozen-lockfile
    displayName: 'Install dependencies and build'
  - script: yarn test-unit --forbid-only
    displayName: 'Unit tests'
  - script: yarn lint
    displayName: 'Lint'

- job: Windows
  pool:
    vmImage: 'windows-2019'
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'
  - task: CacheBeta@1
    inputs:
      key: yarn2 | $(Agent.OS) | yarn.lock
      path: node_modules
    displayName: Cache node modules
  - script: yarn --frozen-lockfile
    displayName: 'Install dependencies and build'
  - script: yarn test-unit --forbid-only
    displayName: 'Unit tests'
  - script: yarn lint
    displayName: 'Lint'

- job: Linux_IntegrationTests
  pool:
    vmImage: 'ubuntu-18.04'
  steps:
  - script: |
      # source: https://github.com/microsoft/playwright/issues/1041
      sudo apt update
      sudo apt install libwoff1 libopus0 libwebp6 libwebpdemux2 libenchant1c2a libgudev-1.0-0 libsecret-1-0 libhyphen0 libgdk-pixbuf2.0-0 libegl1 libnotify4 libxslt1.1 libevent-2.1-6 libgles2 libgl1 libegl1 libvpx5
      # for chromium
      sudo apt install libnss3 libxss1 libasound2
      # for firefox
      sudo apt install libdbus-glib-1-2 libxt6
    displayName: Install required packages
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'
  - task: YarnInstaller@3
    inputs:
      versionSpec: '1.x'
    displayName: 'Install Yarn'
  - script: yarn --frozen-lockfile
    displayName: 'Install dependencies and build'
  - script: yarn test-api-chromium --headless --forbid-only
    displayName: 'Integration tests (Chromium)'
  - script: xvfb-run --auto-servernum -- bash -c "yarn test-api-firefox --headless --forbid-only"
    displayName: 'Integration tests (Firefox)'

# Integration tests are too flaky on macOS https://github.com/xtermjs/xterm.js/issues/3590
# - job: macOS_IntegrationTests
#   pool:
#     vmImage: 'macOS-10.15'
#   steps:
#   - task: NodeTool@0
#     inputs:
#       versionSpec: '14.x'
#     displayName: 'Install Node.js'
#   - script: yarn --frozen-lockfile
#     displayName: 'Install dependencies and build'
#   - script: yarn test-api-chromium --headless --forbid-only
#     displayName: 'Integration tests (Chromium)'
#   - script: yarn test-api-firefox --headless --forbid-only
#     displayName: 'Integration tests (Firefox)'
#   - script: yarn test-api-webkit --headless --forbid-only
#     displayName: 'Integration tests (Webkit)'

- job: Windows_IntegrationTests
  pool:
    vmImage: 'windows-2019'
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'
  - script: yarn --frozen-lockfile
    displayName: 'Install dependencies and build'
  - script: yarn test-api-chromium --headless --forbid-only
    displayName: 'Integration tests (Chromium)'
  - script: yarn test-api-firefox --headless --forbid-only
    displayName: 'Integration tests (Firefox)'

- job: Release
  dependsOn:
  - Linux
  - macOS
  - Windows
  - Linux_IntegrationTests
  # - macOS_IntegrationTests
  - Windows_IntegrationTests
  condition: and(succeeded(), or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), eq(variables['FORCE_RELEASE'], 'true')))
  pool:
    vmImage: 'ubuntu-18.04'
  steps:
  - task: NodeTool@0
    inputs:
      versionSpec: '14.x'
    displayName: 'Install Node.js'
  - task: YarnInstaller@3
    inputs:
      versionSpec: '1.x'
    displayName: 'Install Yarn'
  - task: CacheBeta@1
    inputs:
      key: yarn2 | $(Agent.OS) | yarn.lock
      path: node_modules
    displayName: Cache node modules
  - script: yarn --frozen-lockfile
    displayName: 'Install dependencies and build'
  - script: |
      yarn package-headless
      node ./bin/package_headless.js
    displayName: 'Package xterm-headless'
  - script: NPM_AUTH_TOKEN="$(NPM_AUTH_TOKEN)" node ./bin/publish.js
    displayName: 'Package and publish to npm'
