version: 2.1
orbs:
  aws-s3: circleci/aws-s3@2.0.0

jobs:
  install_dependencies:
    docker:
      - image: cimg/node:20.11.0
    working_directory: ~/repo
    steps:
      - restore_cache:
          keys:
            - source-{{ .Branch }}-{{ .Revision }}
            - source-{{ .Branch }}-
            - source-
      - checkout
      - run: npm install
      - persist_to_workspace:
          root: ~/repo
          paths:
            - styleguide
            - dist
            - package.json
      - save_cache:
          key: source-{{ .Branch }}-{{ .Revision }}
          paths:
            - "~/repo"
  test:
    docker:
        - image: cimg/node:20.11.0
    working_directory: ~/repo
    steps:
      - restore_cache:
          keys:
            - source-{{ .Branch }}-{{ .Revision }}
            - source-{{ .Branch }}-
            - source-
      - run: 
          name: Checking Lint in src/
          command: npm run lint
      - run: npm run test -- -u
  build:
    docker:
        - image: cimg/node:20.11.0
    working_directory: ~/repo
    steps:
      - restore_cache:
          keys:
            - source-{{ .Branch }}-{{ .Revision }}
            - source-{{ .Branch }}-
            - source-
      - run:
          name: Build styleguide
          command: npm run styleguide:build
      - persist_to_workspace:
          root: "~/repo"
          paths:
            - styleguide
            - package.json
  deploy_develop:
    docker:
      - image: cimg/node:20.11.0
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: "~/repo"
      - aws-s3/sync:
          from: styleguide
          to: 's3://generation-ui-components.deployment.int/develop'
          arguments: |
            --acl public-read \
            --cache-control "max-age=86400"
  deploy_production:
    docker:
      - image: cimg/node:20.11.0
    working_directory: ~/repo
    steps:
      - attach_workspace:
          at: "~/repo"
      - aws-s3/sync:
          from: styleguide
          to: 's3://generation-ui-components.deployment.int/production'
          arguments: |
            --acl public-read \
            --cache-control "max-age=86400" 
      - persist_to_workspace:
          root: "~/repo"
          paths:
            - package.json
  release:
    docker:
        - image: cimg/node:20.11.0
    steps:
      - checkout
      - run: npm install
      - run:
          name: Build project in a minified and transpiled version
          command: npm run semantic-release

# Workflow definition for concurrency
workflows:
  version: 2
  test_build_deploy_release:
    jobs:
      - install_dependencies
      - test:
          requires:
            - install_dependencies
      # Deploys depending of the branch
      - build: 
          requires:
            - install_dependencies
      - deploy_develop:
          requires:
            - test
            # - build
          filters:
            branches:
              only:
              - develop
      - deploy_production:
          requires:
            - test
            # - build
          filters:
            branches:
              only:
              - master
      - release:
          requires:
            - test
            # - build
          filters:
            branches:
              only:
              - master
