# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
machine:
    environment:
      PATH: "${PATH}:${HOME}/${CIRCLE_PROJECT_REPONAME}/node_modules/.bin"

defaults: &defaults
  working_directory: ~/app
  docker:
    - image: cypress/base:10
      environment:
          ## this enables colors in the output
          TERM: xterm

jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run: yarn config set workspaces-experimental true
      - run: yarn
      - run: yarn run bootstrap
      - run: yarn run build
      - run: yarn run coverage
      - save_cache:
          key: amplify-yarn-deps-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache ## cache both yarn and Cypress
      - save_cache:
          key: amplify-ssh-deps-{{ .Branch }}
          paths:
            - ~/.ssh
      - persist_to_workspace:
          root: .
          paths: .
  unit_test:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ./
      - run: yarn run test --scope @aws-amplify/auth
      - run: yarn run test --scope @aws-amplify/api
      - run: yarn run test --scope @aws-amplify/analytics
      - run: yarn run test --scope @aws-amplify/cache
      - run: yarn run test --scope @aws-amplify/core
      - run: yarn run test --scope @aws-amplify/interactions
      - run: yarn run test --scope @aws-amplify/pubsub
      - run: yarn run test --scope @aws-amplify/storage
      - run: yarn run test --scope @aws-amplify/xr
      - run: yarn run test --scope aws-amplify-react
      - run: yarn run test --scope aws-amplify-angular
      - run: yarn run test --scope aws-amplify-vue
  integration_test_auth:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ./
      - restore_cache:
          key: amplify-yarn-deps-{{ .Branch }}-{{ checksum "yarn.lock" }}
      - run:
          name: "Link aws-amplify"
          command: |
            cd packages/aws-amplify
            yarn link
      - run:
          name: "Link aws-amplify-react"
          command: |
            cd packages/aws-amplify-react
            yarn link
      - run: 
          name: "Clone auth test package, initialize mobilehub via cli, and link amplify packages"
          command: |
            mkdir ~/.ssh
            echo $SSH_HOST_PUBLIC_KEY >> ~/.ssh/known_hosts
            cd ~
            git clone $AUTH_CLONE_URL
            cd aws-amplify-cypress-auth
            yarn
            yarn link aws-amplify
            yarn link aws-amplify-react
            echo $AWS_EXPORTS_AUTH > src/aws-exports.js
      - run:
          name: "Start Auth test server in background"
          command: |
            cd ~/aws-amplify-cypress-auth
            yarn start
          background: true
      - run:
          name: "Run cypress tests for aws-amplify-react"
          command: |
            yarn cypress --scope aws-amplify-react
      - store_artifacts:
          path: /root/app/packages/aws-amplify-react/cypress/videos
      - store_artifacts:
          path: /root/app/packages/aws-amplify-react/cypress/screenshots
  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ./
      - restore_cache:
          keys: 
            - amplify-ssh-deps-{{ .Branch }}
            - amplify-ssh-deps
      - run:
          name: "Publish to Amplify Package"
          command: |
            if [ -z "$CIRCLE_PULL_REQUEST" ]; then
              echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
              git config --global user.email $GITHUB_EMAIL
              git config --global user.name $GITHUB_USER
              yarn run publish:$CIRCLE_BRANCH
            else
              echo "Skipping deploy."
            fi

workflows:
  version: 2
  build_test_deploy:
    jobs:
      - build
      - unit_test:
          requires:
            - build
      - integration_test_auth:
          filters:
            branches:
              only:
                - release
                - master
          requires:
            - build
      - deploy:
          filters:
            branches:
              only:
                - release
                - master
                - beta
          requires:
            - unit_test
