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

trigger:
- master

pr:
- master

pool:
  vmImage: ubuntu-latest

resources:
- repo: self

variables:
  npm_config_cache: $(Pipeline.Workspace)/.npm

stages:
- stage: Build
  jobs:
  - job: Build
    displayName: Build & Test
    strategy:
      matrix:
        node_12_x:
          node_version: 12.x
        node_14_x:
          node_version: 14.x
        node_16_x:
          node_version: 16.x
    steps:
    - task: Cache@2
      inputs:
        key: 'npm | "$(Agent.OS)" | package-lock.json'
        restoreKeys: |
          npm | "$(Agent.OS)"
        path: $(npm_config_cache)
      displayName: Cache npm

    - task: NodeTool@0 
      inputs:
        versionSpec: $(node_version)
    - script: |
        npm install
        npm test -- --reporter mocha-junit-reporter
    - task: PublishTestResults@2
      condition: succeededOrFailed()
      inputs:
        testRunner: JUnit
        testResultsFiles: 'test-results.xml'


- stage: Release
  displayName: Release a new version to NPM
  condition:  and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master'))
  jobs:
  - job: Release
    displayName: Release to NPM
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Cache@2
      inputs:
        key: 'npm | "$(Agent.OS)" | package-lock.json'
        restoreKeys: |
          npm | "$(Agent.OS)"
        path: $(npm_config_cache)
      displayName: Cache npm
    - task: Npm@1
      inputs: # Ensure that the semantic-release tool is installed as specified in the package.json
        command: 'install'
    - task: Bash@3
      inputs:
        targetType: 'inline'
        script: |
          npx semantic-release
      env:
        GH_TOKEN: $(GH_TOKEN)
        NPM_TOKEN: $(NPM_TOKEN)