version: 2.1

aliases:
  - &yarn-install |
    yarn install --non-interactive --frozen-lockfile --cache-folder ~/.cache/yarn

  - &restore-yarn-cache
    keys:
      - yarn-{{ .Branch }}-{{ checksum "yarn.lock" }}

  - &save-yarn-cache
    paths:
      - ~/.cache/yarn
    key: yarn-{{ .Branch }}-{{ checksum "yarn.lock" }}

  - &persist-js-to-workspace
    root: .
    paths:
      - node_modules
      - __generated__
      - src

  - &attach-workspace
    at: .

executors:
  default_executor:
    docker:
      - image: circleci/node:10

jobs:
  yarn-install:
    executor: default_executor
    steps:
      - checkout
      - restore_cache: *restore-yarn-cache
      - attach_workspace: *attach-workspace
      - run: *yarn-install
      - save_cache: *save-yarn-cache
      - persist_to_workspace: *persist-js-to-workspace

  jest:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run: yarn run jest

  eslint:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run: yarn run eslint

  prettier:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run: yarn run prettier:check

  audit:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run: yarn audit

  build:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run: yarn run build
      - persist_to_workspace:
          root: .
          paths:
            - lib
            - build

  storybook:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run: yarn run build:storybook

  docusaurus:
    executor: default_executor
    steps:
      - checkout
      - attach_workspace: *attach-workspace
      - run:
          command: *yarn-install
          working_directory: website
      - run:
          command: yarn now-build
          working_directory: website

workflows:
  version: 2
  phasers:
    jobs:
      - yarn-install
      - jest:
          requires:
            - yarn-install
      - eslint:
          requires:
            - yarn-install
      - prettier:
          requires:
            - yarn-install
      - audit:
          requires:
            - yarn-install
      - build:
          requires:
            - yarn-install
      - storybook:
          requires:
            - yarn-install
      - docusaurus:
          requires:
            - build
