version: 2
jobs:
  test:
    docker:
      - image: circleci/node:12-browsers
    steps:
      - checkout
      - restore_cache:
          key: deps-{{ checksum "yarn.lock" }}
      - run:
          name: Login to npm
          command: |
            npm config set "//registry.npmjs.org/:_authToken" $NPM_AUTH
            yarn add codecov
      - run:
          name: Install dependencies
          command: yarn install
      - run:
          name: Run tests
          command: make test
      - store_artifacts:
          path: coverage
      - run:
          name: Run coverage
          command: ./node_modules/.bin/codecov
      - save_cache:
          key: deps-{{ checksum "yarn.lock" }}
          paths:
            - node_modules
  publish:
    docker:
      - image: circleci/node:12-browsers
    steps:
      - checkout
      - run:
          name: Login to npm
          command: npm config set "//registry.npmjs.org/:_authToken" $NPM_AUTH
      - run:
          name: Publish to npm
          command: npm publish .
workflows:
  version: 2
  test_and_publish:
    jobs:
      - test:
          filters:  
            tags: 
              only: /.*/
      - publish:
          requires:
            - test
          filters:
            tags: 
              only: /[0-9]+(\.[0-9]+)*(-.+)?/
            branches:
              ignore: /.*/