ANCHORS:
  node_steps: &node_steps
    steps:
      - checkout
      - run: npm ci
      - run: npm test
      # Check whether "run build" is successful
      - run: npm run build

version: 2
jobs:
  node_12:
    docker:
      - image: circleci/node:12-browsers
    <<: *node_steps

  node_10:
    docker:
      - image: circleci/node:10-browsers
    <<: *node_steps

  node_8:
    docker:
      - image: circleci/node:8-browsers
    <<: *node_steps

  # (from: https://circleci.com/blog/publishing-npm-packages-using-circleci-2-0/)
  npm_publish:
    docker:
    - image: circleci/node:12-browsers
    steps:
      - checkout
      - run: npm ci
      - run:
          name: Authenticate with registry
          command: echo -e "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
      - run: npm publish

workflows:
  version: 2
  node_tests:
    jobs:
      - node_12:
          filters:
            tags:
              only: /.*/
      - node_10:
          filters:
            tags:
              only: /.*/
      - node_8:
          filters:
            tags:
              only: /.*/
      - npm_publish:
          requires:
            - node_12
            - node_10
            - node_8
          filters:
            tags:
              only: /.*/
            branches:
              ignore: /.*/
