defaults: &defaults
  working_directory: ~/javascript-utilities
  docker:
    - image: circleci/node:8.11-browsers

version: 2
jobs:
  install:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-yarn-{{ checksum "yarn.lock" }}
            - v1-yarn
      - run: yarn
      - save_cache:
         key: v1-yarn-{{ checksum "yarn.lock" }}
         paths:
           - node_modules
      - persist_to_workspace:
          root: ~/
          paths:
            - javascript-utilities

  test:
    <<: *defaults
    steps:
      - attach_workspace:
         at: ~/
      # Circle cache magic:
      # - Caches are immutable
      # - If Circle cannot find an exact key match, it falls back to the closest partial match
      # - If multiple partial matches are found, it falls back to the *most recent* partial match
      #
      # Appending an {{ epock }} to a build's cache allows rebuilds to find the most recent branch cache.
      # For new branches, Circle falls back to matching the most recent master cache.
      - restore_cache:
          keys:
            - v4-test-{{ .Branch }} # Match branch builds, favoring the most recent build.
            - v4-test-master        # If there's no branch build match, use the most recent master cache.
      - run: yarn run check
      - save_cache:
         key: v4-test-{{ .Branch }}-{{ epoch }}
         paths:
           - build/cache

workflows:
  version: 2

  check:
    jobs:
      - install
      - test:
         requires:
           - install
