version: 2.1

defaults: &defaults
  working_directory: ~/grommet-ci
  docker:
    - image: cimg/node:18.13.0

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
  build:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Build
          command: yarn build && yarn jsonify
      - persist_to_workspace:
          root: ~/grommet-ci
          paths:
            - ./
  release-stable:
    <<: *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
  release-next-stable:
    <<: *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-next-stable
  release-audit-stable:
    <<: *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-audit-stable
  release-tokens-stable:
    <<: *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-tokens-stable
  release-v2-alpha-stable:
    <<: *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-v2-alpha-stable
  release-hpe-design-tokens-v2-stable:
    <<: *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-hpe-design-tokens-v2-stable
  release-gh-pages:
    <<: *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-gh-pages
  publish:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/grommet-ci
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN_NEW" > ~/grommet-ci/.npmrc
      - run:
          name: Publish package
          command: npm publish
workflows:
  build_test:
    jobs:
      - checkout:
          filters:
            tags:
              only: /.*/
      - lint:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
            branches:
              ignore:
                - stable
                - NEXT-stable
                - AUDIT-stable
                - TOKENS-stable
                - v2-alpha-stable
                - hpe-design-tokens-v2-stable
                - gh-pages
      - build:
          requires:
            - checkout
          filters:
            tags:
              only: /.*/
            branches:
              ignore:
                - stable
                - NEXT-stable
                - AUDIT-stable
                - TOKENS-stable
                - v2-alpha-stable
                - hpe-design-tokens-v2-stable
                - gh-pages
      - release-stable:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - master
      - release-next-stable:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - NEXT
      - release-audit-stable:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - AUDIT
      - release-tokens-stable:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - TOKENS
      - release-v2-alpha-stable:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - v2-alpha
      - release-hpe-design-tokens-v2-stable:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - hpe-design-tokens-v2
      - release-gh-pages:
          requires:
            - lint
            - build
          filters:
            branches:
              only:
                - master
      - publish:
          requires:
            - lint
            - build
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
