version: v1.0
name: JSUI

agent:
  machine:
    type: f1-standard-2
    os_image: ubuntu2404

auto_cancel:
  running:
    when: "branch != 'main'"

fail_fast:
  cancel:
    when: "branch != 'main'"

global_job_config:
  prologue:
    commands:
      - sem-version node 20.17.0
      - checkout
      - npm config set "@fortawesome:registry" https://npm.fontawesome.com/
      - npm config set "//registry.npmjs.org/:_authToken" $MEDIAFLY_NPM_AUTH_TOKEN
      - npm config set "//npm.fontawesome.com/:_authToken" $FONTAWESOME_NPM_AUTH_TOKEN
  secrets:
    - name: engagement-fontawesome-token
    - name: engagement-npm

blocks:
  - name: NPM Dependencies
    dependencies: []
    task:
      jobs:
        - name: npm ci
          commands:
            - cache restore node-modules-$(checksum package-lock.json)
            - cache restore cypress-$(grep -oP '"cypress":\s*"\K[^"]+' package.json | head -1)
            - '(([[ ! -d node_modules || ! -d ~/.cache/Cypress ]]) && npm ci) || echo "node_modules and ~/.cache/Cypress folders already exist"'
            - cache store node-modules-$(checksum package-lock.json) node_modules
            - cache store cypress-$(grep -oP '"cypress":\s*"\K[^"]+' package.json | head -1) ~/.cache/Cypress

  - name: Unit Tests
    dependencies: ['NPM Dependencies']
    task:
      prologue:
        commands:
          - cache restore node-modules-$(checksum package-lock.json)
      jobs:
        - name: Karma
          commands:
            - npm test
            - '[[ -f xmloutput/karma-results.xml ]] && test-results publish xmloutput/karma-results.xml'

  - name: E2E Tests
    dependencies: ['NPM Dependencies']
    task:
      prologue:
        commands:
          - cache restore node-modules-$(checksum package-lock.json)
          - cache restore cypress-$(grep -oP '"cypress":\s*"\K[^"]+' package.json | head -1)
      jobs:
        - name: Cypress
          commands:
            - npm run cypress:ci
      epilogue:
        always:
          commands:
            - '[[ -d cypress/xmloutput ]] && test-results publish cypress/xmloutput'
        on_fail:
          commands:
            - artifact push job cypress/screenshots
            - artifact push job cypress/logs

  - name: Build Docs Site
    dependencies: ['NPM Dependencies']
    task:
      prologue:
        commands:
          - cache restore node-modules-$(checksum package-lock.json)
      jobs:
        - name: npm build
          commands:
            - npm run build
            - artifact push workflow dist


promotions:
  - name: Deploy Docs Site
    auto_promote:
      when: "result = 'passed' and branch = 'main'"
    pipeline_file: promotion-deploy-docs.yml
