defaults: &defaults
  docker:
    - image: circleci/node:10.16
  working_directory: ~/repo
version: 2
jobs:
  setup-environment:
    <<: *defaults
    steps:
      - checkout
      - save_cache:
          key: repo-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/repo
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install NPM Packages
          command: yarn
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - ~/repo/node_modules
  lint-and-test:
    <<: *defaults
    steps:
      - restore_cache:
          key: repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Generate Apollo types
          command: yarn apollo:types:generate
      - run:
          name: Run Linter
          command: yarn lint
      - run:
          name: Run Tests
          command: yarn test
  build:
    <<: *defaults
    steps:
      - restore_cache:
          key: repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Build Project
          command: yarn build:library
  release:
    <<: *defaults
    steps:
      - restore_cache:
          key: repo-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Build Project
          command: yarn build:library
      - run:
          name: Semantic Release
          command: yarn semantic-release || true

workflows:
  version: 2
  build-deploy:
    jobs:
      - setup-environment
      - lint-and-test:
          requires:
            - setup-environment
      - build:
          requires:
            - lint-and-test
      - release:
          requires:
            - build
          filters:
            branches:
              only: master
