aliases:
  - &restore-yarn-cache
    keys:
      - v1-yarn-{{ .Branch }}-{{ checksum "yarn.lock" }}
      # Fallback in case checksum fails
      - v1-yarn-{{ .Branch }}-

  - &save-yarn-cache
    paths:
      - node_modules
      - ~/.cache/yarn
    key: v1-yarn-{{ .Branch }}-{{ checksum "yarn.lock" }}

  - &filter-only-master
    branches:
      only:
        - master

  - &filter-ignore-gh-pages
    branches:
      ignore: gh-pages

  - &yarn
    |
      yarn install --non-interactive --cache-folder ~/.cache/yarn

defaults: &defaults
  working_directory: ~/docusaurus
  docker:
    - image: circleci/node:6

version: 2
jobs:
  tests:
    <<: *defaults
    steps:
      - checkout
      - restore-cache: *restore-yarn-cache
      - run: *yarn
      - save-cache: *save-yarn-cache
      - run:
          name: Check Prettier
          command: yarn ci-check
      - run:
          name: Run Test Suites
          command: yarn test
      - run:
          name: Test Static Website Builds
          command: cd website && yarn run build

  # The CIRCLE_ variables are defined during the CircleCI build process
  # https://circleci.com/docs/1.0/environment-variables/
  deploy-website:
    <<: *defaults
    steps:
      - checkout
      - restore-cache: *restore-yarn-cache
      - run: *yarn
      - save-cache: *save-yarn-cache
      - run:
          name: Configure GitHub Bot
          # Do not do this if we don't have the right org (facebook), or if this is just a pull request
          command: |
            if [[ $CIRCLE_PROJECT_USERNAME == "facebook" && -z $CI_PULL_REQUEST && -z $CIRCLE_PR_USERNAME ]]; then
              git config --global user.email "docusaurus-bot@users.noreply.github.com"
              git config --global user.name "Website Deployment Script"
              echo "machine github.com login docusaurus-bot password $DOCUSAURUS_PUBLISH_TOKEN" > ~/.netrc
            fi
      - run:
          name: Deploy Website
          # Skip the deploy if we don't have the right org (facebook), or if this is just a pull request
          command: |
            if [[ $CIRCLE_PROJECT_USERNAME == "facebook" && -z $CI_PULL_REQUEST && -z $CIRCLE_PR_USERNAME ]]; then
              echo "Deploying website..."
              cd website && GIT_USER=docusaurus-bot USE_SSH=false yarn run publish-gh-pages
            else
              echo "Skipping deploy."
            fi

  publish-npm:
    <<: *defaults
    steps:
      - checkout
      - restore-cache: *restore-yarn-cache
      - run: *yarn
      - save-cache: *save-yarn-cache
      - run:
        name: Publish Docusaurus Package
        command: |
          if [ -z "$CIRCLE_PULL_REQUEST" ]; then
            echo "//registry.npmjs.org/:_authToken=${CIRCLE_NPM_TOKEN}" > ~/.npmrc
            node ./scripts/publish-npm.js
          else
            echo "Skipping publication."
          fi

# Workflows enables us to run multiple jobs in parallel
workflows:
  version: 2

  website:
    jobs:
      - tests:
          filters: *filter-ignore-gh-pages
      - deploy-website:
          requires:
            - tests
          filters: *filter-only-master

  deploy:
    jobs:
      - hold:
          type: approval
      - publish-npm:
          requires:
            - hold
          filters: *filter-only-master
