version: 2.1
orbs:
  node: circleci/node@1.1.6
jobs:
  build-and-test:
    executor:
      name: node/default
    steps:
      - checkout
      - node/with-cache:
          steps:
            - run: npm install
            - run: 
                name: Run tests with JUnit as reporter
                command: ./node_modules/.bin/jest --ci --runInBand --reporters=default --reporters=jest-junit --coverage
                environment:
                    JEST_JUNIT_OUTPUT_DIR: ./reports/junit/
            - store_test_results:
                path: ./reports/junit/
            - store_artifacts:
                path: ./reports/junit/
            - run:
                name: Publish coverage in codecov
                command: ./node_modules/.bin/codecov ./coverage/coverage.json
  deploy:
    working_directory: .
    docker:
      - image: circleci/node:8.9.1
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ./.npmrc
      - run:
          name: Publish package
          command: npm publish
workflows:
    build-and-test:
      jobs:
        - build-and-test
        - deploy:
            requires:
              - build-and-test
            filters:
              tags:
                only: /^v.*/
              branches:
                ignore: /.*/