version: 2.1

executors:
  node:
    docker:
      - image: circleci/node:10.17.0

commands:
  install-dependencies:
    description: 'Install Dependencies'
    steps:
      - run:
          name: Install Dependencies
          command: yarn install --frozen-lockfile

  restore-yarn:
    description: 'Restore Yarn Package Cache'
    steps:
      - restore_cache:
          key: yarn-packages-{{ checksum "yarn.lock" }}

  save-yarn:
    description: 'Save Yarn Package Cache'
    steps:
      - save_cache:
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

jobs:
  setup:
    executor: node
    steps:
      - checkout
      - restore-yarn
      - install-dependencies
      - save-yarn
      - persist_to_workspace:
          root: ./
          paths:
            - ./

  run-unit-tests:
    executor: node
    steps:
      - attach_workspace:
          at: ./
      - run:
          name: Run Prettier (Check)
          command: yarn prettier-check
      - run:
          name: Run Jest
          command: yarn test --coverage --runInBand

  build-ts:
    executor: node
    steps:
      - attach_workspace:
          at: ./
      - run:
          name: 'Build TS'
          command: yarn build

workflows:
  version: 2.1

  run-tests:
    jobs:
      - setup
      - run-unit-tests:
          requires:
            - setup
      - build-ts:
          requires:
            - setup

version: 2.1

executors:
  node:
    docker:
      - image: circleci/node:10.17.0

commands:
  install-dependencies:
    description: 'Install Dependencies'
    steps:
      - run:
          name: Install Dependencies
          command: yarn install --frozen-lockfile

  restore-yarn:
    description: 'Restore Yarn Package Cache'
    steps:
      - restore_cache:
          key: yarn-packages-{{ checksum "yarn.lock" }}

  save-yarn:
    description: 'Save Yarn Package Cache'
    steps:
      - save_cache:
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

jobs:
  setup:
    executor: node
    steps:
      - checkout
      - restore-yarn
      - install-dependencies
      - save-yarn
      - persist_to_workspace:
          root: ./
          paths:
            - ./

  run-unit-tests:
    executor: node
    steps:
      - attach_workspace:
          at: ./
      - run:
          name: Run Prettier (Check)
          command: yarn prettier-check
      - run:
          name: Run Jest
          command: yarn test --coverage --runInBand
      - store_artifacts:
          path: ./coverage
          destination: coverage

  build-ts:
    executor: node
    steps:
      - attach_workspace:
          at: ./
      - run:
          name: 'Build TS'
          command: yarn build

workflows:
  version: 2.1

  run-tests:
    jobs:
      - setup
      - run-unit-tests:
          requires:
            - setup
      - build-ts:
          requires:
            - setup

Store coverage report as artifact on Circle
Store coverage report as artifact on Circle
