defaults: &defaults
  working_directory: ~/code
  docker:
    - image: circleci/node:12.9.1-browsers

version: 2
jobs:
  setup:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          name: Restore node modules
          keys:
            - node_modules-{{ arch }}-{{ checksum "yarn.lock" }}

      - run:
          name: Install dependencies
          command: yarn --pure-lockfile

      - save_cache:
          name: Save node modules
          key: node_modules-{{ arch }}-{{ checksum "yarn.lock" }}
          paths:
            - node_modules

  tests:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          name: Restore node modules
          keys:
            - node_modules-{{ arch }}-{{ checksum "yarn.lock" }}

      - run:
          name: Lint checks
          command: yarn ci

  publish:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          name: Restore node modules
          keys:
            - node_modules-{{ arch }}-{{ checksum "yarn.lock" }}

      - run:
          name: Publish to NPM
          command: yarn ci:publish

workflows:
  version: 2
  ci:
    jobs:
      - setup
      - tests:
          requires:
            - setup
      - publish:
          requires:
            - tests
          filters:
            branches:
              only: master