version: 2

node_test_env: &node_test_env
  NPM_CONFIG_UNSAFE_PERM: true


cache_1: &cache_1
  key: npm-cache-01-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}-F267A71D
  paths:
    - ./node_modules
    - ./package-lock.json
    - packages/opentelemetry-api/node_modules
    - packages/opentelemetry-context-async-hooks/node_modules
    - packages/opentelemetry-context-base/node_modules
    - packages/opentelemetry-context-zone/node_modules
    - packages/opentelemetry-context-zone-peer-dep/node_modules
    - packages/opentelemetry-core/node_modules
    - packages/opentelemetry-exporter-jaeger/node_modules
    - packages/opentelemetry-exporter-prometheus/node_modules
    - packages/opentelemetry-exporter-zipkin/node_modules
    - packages/opentelemetry-metrics/node_modules
    - packages/opentelemetry-node/node_modules
    - packages/opentelemetry-shim-opentracing/node_modules
    - packages/opentelemetry-tracing/node_modules
    - packages/opentelemetry-web/node_modules

cache_2: &cache_2
  key: npm-cache-02-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}-F267A71D
  paths:
    - packages/opentelemetry-plugin-grpc/node_modules
    - packages/opentelemetry-plugin-http/node_modules
    - packages/opentelemetry-plugin-https/node_modules
    - packages/opentelemetry-exporter-collector/node_modules
    - packages/opentelemetry-plugin-xml-http-request/node_modules
    - packages/opentelemetry-resources/node_modules

node_unit_tests: &node_unit_tests
  resource_class: large
  steps:
    - checkout
    - run:
        name: Create Checksum
        command: sh .circleci/checksum.sh /tmp/checksums.txt
    - run:
        name: Setup environment variables
        command: |
          echo "export CIRCLE_NODE_VERSION=\$(node --version | grep -oE 'v[0-9]+')" >> $BASH_ENV
          source $BASH_ENV
    - run:
        name: Log out node.js version
        command: |
          node --version
          echo "CIRCLE_NODE_VERSION=${CIRCLE_NODE_VERSION}"
    - restore_cache:
        keys:
          - npm-cache-01-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}-F267A71D
    - restore_cache:
        keys:
          - npm-cache-02-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}-F267A71D
    - run:
        name: Install Root Dependencies
        command: npm install --ignore-scripts
    - run:
        name: Boostrap dependencies
        command: npx lerna bootstrap --no-ci
    - save_cache:
        <<: *cache_1
    - save_cache:
        <<: *cache_2
    - run:
        name: Unit tests
        command: npm run test
    - run:
        name: report coverage
        command: if [ "${CIRCLE_NODE_VERSION}" = "v12" ]; then npm run codecov; fi

browsers_unit_tests: &browsers_unit_tests
  resource_class: large
  steps:
    - checkout
    - run:
        name: Create Checksum
        command: sh .circleci/checksum.sh /tmp/checksums.txt
    - run:
        name: Setup environment variables
        command: |
          echo "export CIRCLE_NODE_VERSION=\$(node --version | grep -oE 'v[0-9]+')" >> $BASH_ENV
          source $BASH_ENV
    - run:
        name: Log out node.js version
        command: |
          node --version
          echo "CIRCLE_NODE_VERSION=${CIRCLE_NODE_VERSION}"
    - restore_cache:
        keys:
          - npm-cache-01-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}-F267A71D
    - restore_cache:
        keys:
          - npm-cache-02-{{ .Environment.CIRCLE_JOB }}-{{ checksum "/tmp/checksums.txt" }}-F267A71D
    - run:
        name: Install Root Dependencies
        command: npm install --ignore-scripts
    - run:
        name: Boostrap dependencies
        command: npx lerna bootstrap --no-ci
    - save_cache:
        <<: *cache_1
    - save_cache:
        <<: *cache_2
    - run:
        name: Unit tests
        command: npm run test:browser
    - run:
        name: report coverage
        command: if [ "$CIRCLE_NODE_VERSION" = "v12" ]; then npm run codecov:browser; fi

jobs:
  node8:
    docker:
      - image: node:8
        environment: *node_test_env
    <<: *node_unit_tests
  node10:
    docker:
      - image: node:10
        environment: *node_test_env
    <<: *node_unit_tests
  node12:
    docker:
      - image: node:12
        environment: *node_test_env
    <<: *node_unit_tests
  node14:
    docker:
      - image: node:14
        environment: *node_test_env
    <<: *node_unit_tests
  node12-browsers:
    docker:
      - image: circleci/node:12-browsers
    <<: *browsers_unit_tests

workflows:
  version: 2
  build:
    jobs:
      - node8
      - node10
      - node12
      - node14
      - node12-browsers

