version: 2.0
# orbs:
#   codecov: codecov/codecov@1.0.2
# List of jobs
jobs:
    # The build job
    build:
        working_directory: ~/project
        docker:
            - image: circleci/node:10.15-browsers
        steps:
          # Steps to be added
          # Checkout the code from the branch into the working_directory
          - checkout
          # Log the current branch
          - run:
              name: Show current branch
              command: echo ${CIRCLE_BRANCH}
          # Restore local dependencies from cache
          - restore_cache:
              keys:
              - v1-dependencies-{{ .Branch }}-{{ checksum "package.json" }}
              - v1-dependencies-{{ checksum "package.json" }}
              - v1-dependencies-
          # Install project dependencies
          - run:
              name: Install local dependencies
              command: yarn install
          # Cache local dependencies if they don't exist
          - save_cache:
              key: v1-dependencies-{{ .Branch }}-{{ checksum "package.json" }}
              paths:
                  - node_modules
          # Set codecov env vars (should be ignored if configured on web)
          # Upload file report to codecov to make codecov available
          - run:
              name: Test and Upload Report to CodeCov
              command: yarn run cov
          # Lint the source code
          # - run:
          #     name: Linting
          #     command: npm run lint
          # Build project with different configuration based on
          # the current branch
          - run:
              name: Building
              command: |
                  if [ "${CIRCLE_BRANCH}" == "staging" ]; then
                      yarn run build-qa
                  elif [ "${CIRCLE_BRANCH}" == "master" ]; then
                      yarn run build
                  else
                      yarn run build-dev
                  fi
          # Cache the dist folder for the deploy job. Finally, we save the dist folder into the cache so we can restore it later in the deploy job
          - save_cache:
              key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
              paths:
                  - dist
    # The deploy job
    deploy:
        working_directory: ~/project
        docker:
            - image: circleci/node:10.15-browsers
        steps:
            # Steps to be added
            # Log the current branch
            - run:
                name: Show current branch
                command: echo ${CIRCLE_BRANCH}
            # Restore cache from the build job which contains the
            # dist folder that needs to be deployed
            - restore_cache:
                key: v1-dist-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_SHA1 }}
            # Install AWS cli
            # - run:
            #     name: Install aws cli
            #     command:
            #         sudo apt-get -y -qq install awscli
            # Set the signature version for the S3 auth
            # - run:
            #     name: Setting Signature Version 4 for S3 Request Authentication
            #     command: aws configure set default.s3.signature_version s3v4
            # Deploy to the S3 bucket corresponding to the current branch
            # - run:
            #     name: Deploy to S3
            #     command: |
            #         if [ "${CIRCLE_BRANCH}" == "develop" ]; then
            #             aws --region eu-west-2 s3 sync dist s3://project-dev/ --delete
            #         elif [ "${CIRCLE_BRANCH}" == "staging" ]; then
            #             aws --region eu-west-2 s3 sync dist s3://project-qa/ --delete
            #         elif [ "${CIRCLE_BRANCH}" == "master" ]; then
            #             aws --region eu-west-2 s3 sync dist s3://project/ --delete
            #         fi
            
workflows:
    version: 2
    # The build and deploy workflow
    build_and_deploy:
        jobs:
            - build
            # - deploy