# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
references:
  env_config: &env_config
    docker:
      # specify the version you desire here
      - image: circleci/node:14

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mongo:3.4.4

    working_directory: ~/repo

jobs:
  set_up_env:
    <<: *env_config

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "yarn.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: yarn install --frozen-lockfile

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "yarn.lock" }}

      - persist_to_workspace:
          root: .
          paths:
            - .

  build:
    <<: *env_config
    steps:
      - attach_workspace:
          at: .
      - run: yarn build

  test:
    <<: *env_config
    steps:
      - attach_workspace:
          at: .
      - run: yarn test
      # - run: yarn coveralls # TODO: make sure that circleci is set up
      #                       # with the correct coveralls keys / env variables

workflows:

  version: 2

  build_and_test:
    jobs:
      - set_up_env
      - build:
          requires:
            - set_up_env
      - test:
          requires:
            - set_up_env
