version: 2.1
default: &default
  working_directory: ~/repo
  docker:
    - image: circleci/node:10.15
commands:
  prepare:
    description: "Prepare working directory"
    steps:
      - checkout
      - 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" }}
jobs:
  build:
    <<: *default
    steps:
      - prepare
      - run:
          name: Build
          command: npm run build
      - run:
          name: Run tests
          command: npm run test
  deploy:
    <<: *default
    steps:
      - prepare
      - run:
          name: Bump version
          command: |
            git config user.email circleci@circleci
            git config user.name CircleCI
            git checkout -- package-lock.json
            npm version patch -m "patch version bump [ci skip]"
            git push
            git push --tags
      - run:
          name: Publish
          command: |
            git status
            echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
            npm publish --access public
workflows:
  version: 2.1
  build-flow:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
      - deploy:
          filters:
            branches:
              only: master
