version: 2.1

defaults: &defaults
  working_directory: ~/node-redisai-js
  docker:
    - image: circleci/node:10.16.3
    - image: redisai/redisai:edge

jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: update-npm
          command: 'sudo npm install -g npm@5'
      - restore_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
      - run:
          name: install-npm-wee
          command: npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package-lock.json" }}
          paths:
            - ./node_modules
      - run:
          name: test
          command: npm test
      - store_artifacts:
          path: test-results.xml
          prefix: tests
      - store_test_results:
          path: test-results.xml
      - persist_to_workspace:
          root: ~/node-redisai-js
          paths: .

  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/node-redisai-js
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > ~/repo/.npmrc
      - run:
          name: Publish package
          command: npm publish


workflows:
  version: 2

  test-deploy:
    jobs:
      - build:
          filters:
            tags:
              only: /^v.*/
      - deploy:
          requires:
            - build
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
  commit:
    jobs:
      - build
  nightly:
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - build