version: 2

node12: &node12
  working_directory: ~/c3
  docker:
    - image: circleci/node:12-browsers

node14: &node14
  working_directory: ~/c3
  docker:
    - image: circleci/node:14-browsers

restore_modules_cache: &restore_modules_cache
  restore_cache:
    keys:
    - npm-4-{{ checksum "package.json" }}
    # fallback to using the latest cache if no exact match is found
    - npm-4-

save_modules_cache: &save_modules_cache
  save_cache:
    key: npm-4-{{ checksum "package.json" }}
    paths: ./node_modules

install_and_test: &install_and_test
  steps:
    - checkout
    - run:
        name: Display versions
        command: |
          echo "node $(node -v)"
          echo "npm v$(npm --version)"
          echo "$(google-chrome --version)"
    - *restore_modules_cache
    - run:
        name: Installing Dependencies
        command: yarn
    - *save_modules_cache
    - run: yarn test
    - run: yarn codecov
    - store_artifacts:
        path: htdocs
        destination: htdocs
    - run: npx status-back -s -c circleci/htdocs -r c3js/c3 "preview build succes!" "https://${CIRCLE_BUILD_NUM}-11496279-gh.circle-artifacts.com/0/htdocs/index.html"

jobs:
  test_on_node12:
    <<: *node12
    <<: *install_and_test

  test_on_node14:
    <<: *node14
    <<: *install_and_test

  docs:
    docker:
      - image: circleci/ruby:2.4-node
        env:
          BUNDLE_PATH: vendor/bundle
    steps:
      - checkout
      - restore_cache:
          key: deps-bundle-{{ checksum "Gemfile.lock" }}
      - run: bundle install
      - save_cache:
          key: deps-bundle-{{ checksum "Gemfile.lock" }}
          paths:
            - vendor/bundle
      - *restore_modules_cache
      - run: yarn
      - *save_modules_cache
      - run: yarn build
      - run: yarn copy-to-docs
      - run: yarn build:docs
      - store_artifacts:
          path: build
          destination: docs

workflows:
  version: 2
  test:
    jobs:
      - test_on_node12
      - test_on_node14
      - docs
