# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2

# Aliases are shorthand references for common settings.
aliases:
  # Docker Images
  - &use-github
    - image: cibuilds/github:0.10
      environment:
        GIT_AUTHOR_NAME: iubot
        GIT_AUTHOR_EMAIL: iubot@iu.edu
        GIT_COMMITTER_NAME: iubot
        GIT_COMMITTER_EMAIL: iubot@iu.edu

  # Cache Management
  - &restore-node-modules
    keys:
      - v2-node-modules-{{ checksum "package.json" }}

  - &save-node-modules
    paths:
      - node_modules
    key: v2-node-modules-{{ checksum "package.json" }}

  # Branch Filtering
  - &filter-only-master
    branches:
      only:
        - master

  - &filter-only-release-hotfix
    branches:
      only:
        - /^release.*/
        - /^hotfix.*/

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

defaults: &defaults
  working_directory: ~/rivet-switch
  docker:
    - image: circleci/node:12.1.0

jobs:
  build:
    <<: *defaults

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache: *restore-node-modules

      # Install dependencies
      - run:
          name: Install dependencies
          command: npm install

      # Save cache settings
      - save_cache: *save-node-modules

      # Build!
      - run:
          name: Build components
          command: npm run build

  test:
    working_directory: ~/rivet-switch

    docker:
      - image: cypress/base:8

    environment:
      ## this enables colors in the output
      TERM: xterm

    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache: *restore-node-modules

      # Install dependencies
      - run:
          name: Install dependencies
          command: npm install

      # Save cache settings
      - save_cache: *save-node-modules

      - run:
          command: npm run headless
          background: true

      # Run tests
      - run:
          name: Run tests
          command: npm test

  publish_rc_package:
    <<: *defaults
    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache: *restore-node-modules
      # Install dependencies
      - run:
          name: Install dependencies
          command: npm install
      # Save cache settings
      - save_cache: *save-node-modules
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/$CIRCLE_PROJECT_REPONAME/.npmrc
      - run:
          name: Publish RC package
          command: bash scripts/publish_rc.sh

  publish_production_package:
    <<: *defaults
    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache: *restore-node-modules
      # Install dependencies
      - run:
          name: Install dependencies
          command: npm install
      # Save cache settings
      - save_cache: *save-node-modules
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/$CIRCLE_PROJECT_REPONAME/.npmrc
      - run:
          name: Publish production package
          command: bash scripts/publish_prod.sh

  publish_docs:
    <<: *defaults
    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache: *restore-node-modules
      # Install dependencies
      - run:
          name: Install dependencies
          command: npm install
      # Save cache settings
      - save_cache: *save-node-modules
      - run:
          name: Publish docs to gh-pages
          command: |
            git config --global user.email "iubot@iu.edu"
            git config --global user.name "iubot"
            npm run deploy

workflows:
  version: 2
  build-test:
    jobs:
      - build:
          filters: *filter-ignore-gh-pages
      - test:
          requires:
            - build
      - publish_rc_package:
          requires:
            - test
          filters: *filter-only-release-hotfix
          context: iubot
      - publish_production_package:
          requires:
            - test
          filters: *filter-only-master
          context: iubot
      - publish_docs:
          requires:
            - publish_production_package
          filters: *filter-only-master
          context: iubot
