version: 2

defaults: &defaults
  working_directory: ~/storybook-addon-jsx
  docker:
    - image: circleci/node:latest-browsers

jobs:
  install:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            # Find a cache corresponding to this specific package.json checksum
            # when this file is changed, this key will fail
            - storybook-addon-jsx-{{ .Branch }}-{{ checksum "yarn.lock" }}-{{ checksum ".circleci/config.yml" }}
            - storybook-addon-jsx-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - storybook-addon-jsx-{{ .Branch }}
            # Find the most recent cache used from any branch
            - storybook-addon-jsx-master
            - storybook-addon-jsx-
      - run:
          name: Install Dependencies
          command: yarn install --frozen-lockfile
      - save_cache:
          key: storybook-addon-jsx-{{ .Branch }}-{{ checksum "yarn.lock" }}-{{ checksum ".circleci/config.yml" }}
          paths:
            - node_modules
            - ~/.cache/yarn
      - persist_to_workspace:
          root: .
          paths:
            - .

  test:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/storybook-addon-jsx
      - run:
          name: Test
          command: yarn test

  lint:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/storybook-addon-jsx
      - run:
          name: Lint
          command: yarn lint

  build:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/storybook-addon-jsx
      - run:
          name: Build
          command: yarn build
      - persist_to_workspace:
          root: .
          paths:
            - .

  release:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/storybook-addon-jsx
      - run:
          name: Release
          command: yarn run release

workflows:
  version: 2
  build_and_test:
    jobs:
      - install

      - test:
          requires:
            - install

      - lint:
          requires:
            - install

      - build:
          requires:
            - install

      - release:
          requires:
            - lint
            - test
            - build
