version: 2
jobs:
  build:
    name: Build
    docker:
      - image: circleci/node:latest

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v6-dependencies-{{ checksum "package.json" }}

      - run: npm install
      - run: npm install @syncano/cli@canary --no-save
      - run: npm run build
      - run: npx s compile

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

      - save_cache:
          key: v6-repo-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/repo

  test:
    name: Tests
    docker:
      - image: circleci/node:latest

    working_directory: ~/repo

    steps:
      - restore_cache:
          key: v6-repo-{{ .Environment.CIRCLE_SHA1 }}
      - run: npm run test

      - store_artifacts:
          path: ~/repo/test/.results
          destination: results

      - run: npm run test
      - run: npm run codecov

  deploy_staging:
    name: Deploy
    docker:
      - image: circleci/node:latest

    working_directory: ~/repo

    steps:
      - restore_cache:
          key: v6-repo-{{ .Environment.CIRCLE_SHA1 }}
      - run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run: npm publish --tag canary --access=public

  deploy:
    name: Deploy
    docker:
      - image: circleci/node:latest

    working_directory: ~/repo

    steps:
      - restore_cache:
          key: v6-repo-{{ .Environment.CIRCLE_SHA1 }}
      - run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run: npm dist-tag add `npm view . name`@`node -p "require('./package.json').version"` latest

workflows:
  version: 2
  build-test:
    jobs:
      - build
      - test:
          requires:
            - build
      - deploy_staging:
          requires:
            - test
          filters:
            branches:
              only: master
      - deploy:
          requires:
            - test
          filters:
            branches:
              only: /\d/
