version: 2
jobs:
  build:
    docker:
      - image: circleci/node:14-buster
    working_directory: ~/onedrive-api

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run:
          name: configure NPM registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # Ensure Prettier + ESLint has been run on all JS files
      - run: npm run lint

      # run tests!
      - run: npm test

      - deploy:
          name: publish
          command: |
            if [ "${CIRCLE_BRANCH}" == "master" ]; then
              npm publish && npm run release:tag;
            fi
