image: node:24.4.1

clone:
  depth: full

definitions:
  caches:
    sonar: ~/.sonar/cache
  steps:
    - step: &install
        name: Install Dependencies
        caches:
          - node
        script:
          - npm install
        artifacts:
          - node_modules/**
    - step: &test
        name: Unit Tests & Coverage
        size: 2x
        caches:
          - node
        on-fail:
          strategy: ignore
        script:
          - npm run test
        artifacts:
          - test-output/**
    - step: &build
        name: Build Platform
        size: 2x
        caches:
          - node
        script:
          - npm run build
        artifacts:
          - dist/**
    - step: &sonarcloud
        name: Analyze on SonarQube Cloud
        size: 2x
        services:
          - docker
        caches:
          - node
          - sonar
        script:
          - pipe: sonarsource/sonarcloud-scan:4.0.0
            variables:
              EXTRA_ARGS: '-Dsonar.javascript.node.maxspace=7000'
    - step: &check-quality-gate-sonarcloud
        name: Check the Quality Gate on SonarQube Cloud
        size: 2x
        script:
          - pipe: sonarsource/sonarcloud-quality-gate:0.1.6
    - step: &format
        name: Format Code
        script:
          - BITBUCKET_BOT_EMAIL="bitbucket@singular.health"
          - BITBUCKET_BOT_NAME="Bitbucket Bot"
          - |
            if [[ "$(git show -s --format=%ae HEAD)" == "$BITBUCKET_BOT_EMAIL" ]]; then
              echo "Last commit by bot; skipping format."
              exit 0
            fi
          - npm run format
          - |
            if ! git diff --quiet; then
              git config user.email "$BITBUCKET_BOT_EMAIL"
              git config user.name "$BITBUCKET_BOT_NAME"
              git add -A
              git commit -m "chore: format code"
              git remote set-url origin "https://x-token-auth:${BITBUCKET_ACCESS_TOKEN}@bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}.git"
              git push origin "HEAD:${BITBUCKET_BRANCH}"
              echo "Formatting changes required; restarting pipeline."
              exit 1
            else
              echo "No changes to commit."
            fi
  services:
    docker:
      memory: 7000

pipelines:
  branches:
    main:
      - step: *install
      - step: *format
      - parallel:
          steps:
            - step: *test
            - step: *build
      - step: *sonarcloud
      - step: *check-quality-gate-sonarcloud
      - step:
          name: Deploy to NPM Production
          caches:
            - node
          script:
            - export BUCKET_NAME="3cr-playground"
            - export DIST_NAME="E2S3L2TZSMLWP"
            - npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN
            - npm run deploy:ci
            - echo "SERVICE_WORKER_BUILD = \"${BITBUCKET_BUILD_NUMBER}_main\"" >> playground/sw.js
            - echo "SERVICE_WORKER_BUILD_TIME = $(date +%s)" >> playground/sw.js
            - cat playground/sw.js
            - npm run deploy:playground
    development:
      - step: *install
      - step: *format
      - parallel:
          steps:
            - step: *test
            - step: *build
      - step: *sonarcloud
      - step: *check-quality-gate-sonarcloud
      - step:
          name: Deploy to NPM Development
          caches:
            - node
          script:
            - export BUCKET_NAME="3cr-playground-development"
            - export DIST_NAME="EO7C7NEHS1T35"
            - sed -i -e "s:@3cr/viewer\\-browser:@3cr/viewer\\-browser\\-development:g" ./package.json
            - sed -i -e "s:@3cr/viewer\\-browser:@3cr/viewer\\-browser\\-development:g" ./README.md
            - npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN
            - npm run deploy:ci
            - echo "SERVICE_WORKER_BUILD = \"${BITBUCKET_BUILD_NUMBER}_development\"" >> playground/sw.js
            - echo "SERVICE_WORKER_BUILD_TIME = $(date +%s)" >> playground/sw.js
            - cat playground/sw.js
            - npm run deploy:playground
  pull-requests:
    '**':
      - step: *install
      - step: *format
      - parallel:
          steps:
            - step: *test
            - step: *build
      - step: *sonarcloud
      - step: *check-quality-gate-sonarcloud

script:
  - pipe: atlassian/bitbucket-clear-cache:3.6.2
    variables:
    # BITBUCKET_USERNAME: '<string>' # Optional
    # BITBUCKET_APP_PASSWORD: '<string>' # Optional
    # BITBUCKET_ACCESS_TOKEN: '<string>' # Optional
    # CACHES: "<array>"  # Optional
    # DEBUG: "<boolean>" # Optional
    # WORKSPACE: "<string>" # Optional
    # REPO_SLUG: "<string>" # Optional
    # DEBUG: "<string>" # Optional
