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

version: 2.0
jobs:
  install:
    <<: *defaults
    working_directory: ~/repo
    steps:
      - checkout
      - 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
      - run: npm install bslint nyc codecov
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package-lock.json" }}
      - persist_to_workspace:
          root: .
          paths: .

  test:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Tests with coverage
          command: npm run test-ci
      - run:
          name: Submit coverage
          command: ./node_modules/.bin/codecov

  lint:
    <<: *defaults
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Lint
          command: npm run lint-ci

workflows:
  version: 2
  test_and_lint:
    jobs:
      - install
      - lint:
          requires:
            - install
      - test:
          requires:
            - install
