version: 2
jobs:
  build:
    docker:
    - image: node:8
    steps:
    - checkout
    # Download and cache dependencies
    - restore_cache:
        keys:
          - v1-dependencies-{{ checksum "package-lock.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-lock.json" }}
    - run: npm run build
    - persist_to_workspace:
        root: .
        paths:
          - dist/*
  test:
    docker:
    - image: node:8
    steps:
    - checkout
    # Download and cache dependencies
    - restore_cache:
        keys:
          - v1-dependencies-{{ checksum "package-lock.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-
    - attach_workspace:
        at: .
    - run: npm run test
    - run: npm run test_js
  deploy:
    docker:
    - image: node:8
    steps:
    - checkout
    # Download and cache dependencies
    - restore_cache:
        keys:
          - v1-dependencies-{{ checksum "package-lock.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-
    - attach_workspace:
        at: .
    - run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
    - run: npm publish
      

workflows:
  version: 2
  build_and_deploy:
    jobs:
      - build:
          filters:
            tags:
              only: /.*/
      - test:
          requires:
            - build
          filters:
            tags:
              only: /.*/
      - deploy:
          requires:
            - test
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
