version: 2
jobs:
  test:
    docker:
      - image: circleci/node:10
    steps:
      # Configure your test steps here (checkout, npm install, cache management, tests etc...)
      - checkout
      # Download and cache dependencies
      - restore_cache:
          keys:
          - dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - dependencies-
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: dependencies-{{ checksum "package.json" }}
        
      # run tests!
      - run: npm run build
      - run: npm run test
      - run: npx codecov
      - run: npm run lint -- --max-warnings=0

  release:
    docker:
      - image: circleci/node:10
    steps:
      - checkout
      # Download and cache dependencies
      - restore_cache:
          keys:
          - dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - dependencies-
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: dependencies-{{ checksum "package.json" }}

      # release
      - run: npm run release

  publish_documentation:
    docker:
      - image: circleci/node:10
    steps:
      - add_ssh_keys:
            fingerprints:
              - "76:24:8f:b3:d2:7f:9e:2c:be:c1:69:5e:85:36:c4:ed"
      - checkout
      # Download and cache dependencies
      - restore_cache:
          keys:
          - dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - dependencies-
      - run: npm install
      - save_cache:
          paths:
            - node_modules
          key: dependencies-{{ checksum "package.json" }}

      # release
      - run: npm run documentation:build
      - run: npm run documentation:publish

workflows:
  version: 2
  test_and_release:
    # Run the test jobs first, then the release only when all the test jobs are successful
    jobs:
      - test
      - release:
          requires:
            - test
          filters:
            branches:
              only:
                - master
      - publish_documentation:
          requires:
            - release
          filters:
            branches:
              only:
                - master