version: 2
jobs:
  build:
    environment:
      NODE_ENV: development
    docker:
      - image: circleci/node:8
    working_directory: ~/repo
    steps:
      - checkout

      # Update npm
      - run:
          name: update-npm
          command: 'sudo npm install -g npm@latest'

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      - run:
          name: Publish Key
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc

      - run:
          name: Deploy
          command: |
              if [ "${CIRCLE_BRANCH}" == "master" ]; then
                npm publish
              fi