version: 2

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:10

jobs:

  build:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: Install Dependences
          command: yarn install
      - run:
          name: Build Package
          command: yarn build
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
            - ./node_modules
      - persist_to_workspace:
          root: ~/repo
          paths:
            - .

  test:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Eslint
          command: yarn lint
      - run:
          name: Unit Test
          command: yarn test --coverage
      - run:
          name: Upload Coverage
          command: bash <(curl -s https://codecov.io/bash)

  release:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Authenticate With Registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
      - run:
          name: Publish Package
          command: npm publish

  bump:
    <<: *defaults
    steps:
      - add_ssh_keys:
          fingerprints:
            - "b1:ae:28:64:a6:7f:88:27:a9:6c:12:a7:85:d9:4e:03"
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: Install Dependences
          command: yarn install
      - run:
          name: git config
          command: |
            git config --global user.email "robot@circleci.com"
            git config --global user.name "robot"
      - run:
          name: bump version
          command: bin/bump.sh

workflows:
  version: 2
  main:
    jobs:
      - build:
          filters:  # required since `release` has tag filters AND requires `build`
            tags:
              only: /.*/
      - test:
          requires:
            - build
          filters:  # required since `release` has tag filters AND requires `test`
            tags:
              only: /.*/
      - release:
          context: production
          requires:
            - test
          filters:
            tags:
              only: /v.*/
            branches:
              ignore: /.*/
  nightly:
    triggers:
      - schedule:
          cron: "20 20 * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - bump
