version: 2

defaults: &defaults
  docker:
    - image: circleci/node:latest
  working_directory: ~/repo

jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          name: Restore Npm Package Cache
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install Dependencies
          command: npm install
      - save_cache:
          name: Save Npm Package Cache
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: Test
          command: npm run test
      - run:
          name: Build
          command: npm run build
      - persist_to_workspace:
          root: ~/repo
          paths: .
      - store_artifacts:
          path: ./dist
          destination: dist
  release:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
      - run:
          name: Publish package
          command: npm publish

workflows:
  version: 2
  build_and_release:
    jobs:
      - build
      - release:
          requires:
            - build
          filters:
            branches:
              only: master
