version: 2.1

orbs:
  node: circleci/node@4.7.0

jobs:
  test:
    steps:
      - checkout
      - restore_cache:
          keys:
            - devkit-deps-{{ checksum "package.json" }}
      - run: npm install
      - run:
          name: Run tests
          command: npm test
      - save_cache:
          paths:
            - node_modules
          key: devkit-deps-{{ checksum "package.json" }}
      - persist_to_workspace:
          root: ~/repo
          paths:
            - .
  build:
    steps:
      - attach_workspace:
          at: ~/repo
      - run: npm run build
      - persist_to_workspace:
          root: ~/repo
          paths:
            - .
  release:
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Add github.com to known hosts
          command: mkdir ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
      - run:
          name: Versioning
          command: npm version patch -m "version %s [skip ci]"
      - run:
          name: Push tag & changes to github.com
          command: git push
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/.npmrc
      - run:
          name: Publish the package
          command: npm publish

workflows:
  test-deploy:
    jobs:
      - test
      - build:
          requires:
            - test
      - release:
          filters:
            branches:
              only: master
          requires:
            - build
