version: 2.1

defaults: &defaults
  working_directory: ~/grommet-ci
  docker:
    - image: circleci/node:12

jobs:
  checkout:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - yarn-cache-{{ checksum "yarn.lock" }}
      - run: yarn --frozen-lockfile
      - save_cache:
          key: yarn-cache-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache
            - ./node_modules
      - persist_to_workspace:
          root: ~/grommet-ci
          paths:
            - ./
  lint:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Running linter
          command: yarn lint
  jest:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Running jest
          command: yarn test
  chromatic:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Running chromatic test
          command: yarn chromatic test --app-code=9q99in2ygnh --exit-zero-on-changes
  chromatic-accept:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Running chromatic test
          command: yarn chromatic test --app-code=9q99in2ygnh --exit-zero-on-changes --auto-accept-changes
  build:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Build
          command: yarn build
      - persist_to_workspace:
          root: ~/grommet-ci
          paths:
            - ./
  bundlesize:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: size
          command: yarn test-bundlesize
  release:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          command: |
            git config --global user.name "Grommet Community Bot"
            git config --global user.email "grommet@hpe.com"
            yarn release-stable
  publish:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/grommet-ci/dist/.npmrc
      - run:
          name: Add npmignore
          command: echo "**/__tests__/**" > ~/grommet-ci/dist/.npmignore
      - run:
          name: Publish package
          command: cd dist && npm publish
      - run:
          name: Validate publish
          command: cd ~/grommet-ci/templates/grommet-v2-template && yarn install && yarn build
workflows:
  build_test:
    jobs:
      - checkout:
          filters:
            tags:
              only: /.*/
      - lint:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
      - jest:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
      - chromatic:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
            branches:
              ignore:
                - master
      - chromatic-accept:
          requires:
            - checkout
          filters:
            branches:
              only:
                - master
      - build:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
      - bundlesize:
          requires:
            - build
          filters:
            tags:
              only: /.*/
      - release:
          requires:
            - jest
            - chromatic
            - lint
            - build
          filters:
            branches:
              only:
                - master
      - publish:
          requires:
            - jest
            - chromatic
            - lint
            - build
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
