workflows:
  version: 2
  node-multi-build:
    jobs:
      - node-v4
      - node-v6
      - node-v8

version: 2
jobs:
  node-base: &node-base
    docker:
      - image: node
    working_directory: ~/working_directory
    steps:
      - run:
          name: Versions
          command: |
            yarn versions
            echo "npm: $(npm --version)"
      - checkout
      - restore_cache:
          keys:
            - v{{ .Environment.CIRCLE_CACHE_VERSION }}-yarn-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "yarn.lock" }}
            - v{{ .Environment.CIRCLE_CACHE_VERSION }}-yarn-lock-master-{{ .Environment.CIRCLE_JOB }}-{{ checksum "yarn.lock" }}
            - v{{ .Environment.CIRCLE_CACHE_VERSION }}-yarn-cache-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}
            - v{{ .Environment.CIRCLE_CACHE_VERSION }}-yarn-cache-master-{{ .Environment.CIRCLE_JOB }}
      - run:
          name: Install dependencies
          command: yarn install
      - run:
          name: Test
          command: yarn test
      - save_cache:
          key: v{{ .Environment.CIRCLE_CACHE_VERSION }}-yarn-lock-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
      - save_cache:
          key: v{{ .Environment.CIRCLE_CACHE_VERSION }}-yarn-cache-{{ .Branch }}-{{ .Environment.CIRCLE_JOB }}-{{ checksum "yarn.lock" }}
          paths:
            - /usr/local/share/.cache/yarn
            - /root/.cache/yarn
      - run:
          # Variables are not available in `path` or `paths`
          # https://discuss.circleci.com/t/can-i-use-variables-in-cache-paths/11393
          name: Check `yarn cache dir`
          command: |
            CACHE_DIR=$(yarn cache dir)
            echo "yarn cache dir: ${CACHE_DIR}"
            # v0.x or v1.x
            if [[ "$CACHE_DIR" != /usr/local/share/.cache/yarn* && "$CACHE_DIR" != /root/.cache/yarn/* ]]; then
                exit 1
            fi

  node-v4:
    <<: *node-base
    docker:
      - image: 'node:4'
  node-v6:
    <<: *node-base
    docker:
      - image: node:6
  node-v8:
    <<: *node-base
    docker:
      - image: "node:8.9.0"
