version: 2

defaults: &defaults
  docker:
    - image: circleci/node:8.9.1

jobs:
  test:
    <<: *defaults
    steps:
      - checkout

      - restore_cache:
          keys:
            - react-loadable-ssr-addon-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - react-loadable-ssr-addon-

      - run: npm install
      - run:
         name: Run Tests
         command: npm run test

      - save_cache:
          paths:
            - node_modules
          key: react-loadable-ssr-addon-{{ checksum "package.json" }}

      - persist_to_workspace:
          root: .
          paths: .

  publish:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ./.npmrc
      - run: npm publish

workflows:
  version: 2
  main:
    jobs:
      - test:
          filters:
            tags:
              only: /^v.*/
      - publish:
          requires:
            - test
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/