version: 2

jobs:
  preview:
    docker:
      - image: circleci/node:8
    resource_class: small
    steps:
      - checkout
      - run:
          name: "Print out versions"
          command: |
            echo "node -v = $(node -v)"
            echo "npm -v = $(npm -v)"
            echo "yarn --version = $(yarn --version)"

      - restore_cache:
          key: node_modules-{{checksum "./package.json"}}-v2
      - run:
          name: "Install dependencies"
          command: |
            echo "# Top level package"
            npm i
            echo
            for chart in $(make charts-list); do
              echo "# ${chart}"
              pushd charts/${chart} && npm i && popd
              echo
            done
      - save_cache:
          key: node_modules-{{checksum "./package.json"}}-v2
          paths:
            - "./node_modules"

      - run:
          name: "Build preview"
          command: npm run build-storybook
      - run:
          name: "Push preview to the cloud"
          command: |
            echo "* Installing amazon's cli..."
            sudo apt-get update
            sudo apt-get install -y python-pip python-dev
            sudo pip install -U 'awscli>=1.15'

            echo "* Copying to S3..."
            aws s3 sync storybook-static/ s3://charts-preview.density.rodeo/${CIRCLE_SHA1} --region us-east-1
            aws cloudfront create-invalidation --distribution-id E1RS67LHMIGYIQ --paths "/"
      - run:
          name: "Post preview link to any pull requests commit is part of"
          command: |
            echo "* Posting status to commit ${CIRCLE_SHA1}"
            curl -X POST \
            -d "{\"state\":\"success\", \"context\": \"ci-preview\", \"target_url\": \"https://charts-preview.density.rodeo/${CIRCLE_SHA1}\"}" \
            -H "Authorization: Bearer $GITHUB_MACHINE_USER_TOKEN" \
            https://api.github.com/repos/densityco/charts/statuses/${CIRCLE_SHA1}

  publish:
    docker:
      - image: circleci/node:8.11
    resource_class: small
    steps:
      - checkout
      - restore_cache:
          key: node_modules-{{checksum "./package.json"}}
      - run:
          name: "Add npm token to npmrc"
          command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> ~/.npmrc
      - run:
          name: "Publish to npm registry"
          command: |
            for chart in $(make charts-list); do
              echo "****************"
              echo "* Publishing $chart"
              echo "****************"
              set +e
              make $chart-publish
              set -e
              echo
            done

            echo "****************"
            echo "* Publishing main package"
            echo "****************"
            set +e
            make publish
            set -e

workflows:
  version: 2
  npm-deployment:
    jobs:
      # For all pushes, build/deploy to `https://charts-preview.density.rodeo/SHA`
      - preview

      # Trunk is published to npm
      - publish:
          requires:
            - preview
          filters:
            branches:
              only: trunk
