defaults: &defaults
  working_directory: ~/user-agents
  docker:
    - image: circleci/node:8.11.4

whitelist: &whitelist
  paths:
    - .babelrc
    - .circleci/*
    - .eslintrc
    - .git/*
    - .gitignore
    - dist/*
    - CONTRIBUTING.md
    - LICENSE
    - README.md
    - media/*
    - package.json
    - src/*
    - test/*
    - webpack.config.js
    - yarn.lock

version: 2
jobs:
  checkout:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: Install Dependencies
          command: yarn install
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
            - ./node_modules
      - persist_to_workspace:
          root: ~/user-agents
          paths:
          <<: *whitelist

  build:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/user-agents
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: Build
          command: |
            yarn gunzip-data
            yarn build
      - persist_to_workspace:
          root: ~/user-agents
          paths:
          <<: *whitelist

  test:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/user-agents
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: Test
          command: |
            yarn test

  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/user-agents
      - run:
          name: Write NPM Token to ~/.npmrc
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run:
          name: Install dot-json package
          command: npm install dot-json
      - run:
          name: Write version to package.json
          command: $(yarn bin)/dot-json package.json version ${CIRCLE_TAG:1}
      - run:
          name: Publish to NPM
          command: npm publish --access=public

  update:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/user-agents
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: Update the user agents data
          command: |
            echo "$GOOGLE_ANALYTICS_CREDENTIALS" | base64 --decode > ./google-analytics-credentials.json
            yarn update-data
      - persist_to_workspace:
          root: ~/user-agents
          paths:
          <<: *whitelist

  publish-new-version:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/user-agents
      - run:
          name: Commit the newly downloaded data
          command: |
            git add src/user-agents.json.gz
            # Configure some identity details for the machine deployment account.
            git config --global user.email "user-agents@intoli.com"
            git config --global user.name "User Agents"
            git config --global push.default "simple"
            # Disable strict host checking.
            mkdir -p ~/.ssh/
            echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
            # The status code will be 1 if there are no changes,
            # but we want to publish anyway to stay on a regular schedule.
            git commit -m 'Regularly scheduled user agent data update.' || true
      - run:
          name: Bump the patch version and trigger a new release
          command: npm version patch && git push && git push --tags


workflows:
  version: 2

  build:
    jobs:
      - checkout
      - build:
          filters:
            tags:
              ignore: /v[0-9]+(\.[0-9]+)*/
          requires:
            - checkout
      - test:
          filters:
            tags:
              ignore: /v[0-9]+(\.[0-9]+)*/
          requires:
            - build

  release:
    jobs:
      - checkout:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
      - build:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - checkout
      - test:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - build
      - deploy:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - test

  scheduled-release:
    triggers:
      - schedule:
          cron: "00 06 * * *"
          filters:
            branches:
              only:
                - master

    jobs:
      - checkout
      - update:
          requires:
            - checkout
      - build:
          requires:
            - update
      - test:
          requires:
            - build
      - publish-new-version:
          requires:
            - test
