version: 2
jobs:
  build:
    parallelism: 1
    shell: /bin/bash --login
    docker:
      - image: circleci/node:12.3.1-browsers
    steps:
      - checkout
      - run:
          name: Install dependencies
          command: npm install
      - run:
          name: Run JS linter
          command: npm run lint
      - run:
          name: Run CSS linter
          command: npm run stylelint
      - run:
          name: Run tests
          command: npm run test -- --runInBand
      - run:
          name: Build
          command: npm run build
      - run:
          name: Deploy Storybook
          command: |
            if [ "${CIRCLE_BRANCH}" == "develop" ]; then
              curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" &&
              unzip awscli-bundle.zip &&
              sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws &&
              mkdir -p ~/.aws &&
              echo -e "[storybook-deployer]\naws_access_key_id=$AWS_ACCESS_KEY_ID\naws_secret_access_key=$AWS_SECRET_ACCESS_KEY" > ~/.aws/credentials &&
              npm run deploy-storybook
            else
              curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" &&
              unzip awscli-bundle.zip &&
              sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws &&
              mkdir -p ~/.aws &&
              echo -e "[storybook-deployer]\naws_access_key_id=$AWS_ACCESS_KEY_ID\naws_secret_access_key=$AWS_SECRET_ACCESS_KEY" > ~/.aws/credentials &&
              npm run deploy-storybook-branch -- --bucket-path=storybook-fhir-react-lib/dev/${CIRCLE_BRANCH}
              echo "Available here: http://storybook-fhir-react-lib.s3-website-us-east-1.amazonaws.com/dev/${CIRCLE_BRANCH}"
            fi
