# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2.1
jobs:
  build:
    docker:
      - image: circleci/node:8.16-browsers
    
    environment:
      NODE_ENV: production
      JEST_JUNIT_OUTPUT_NAME: results.xml
    working_directory: ~/repo

    steps:
      - restore_cache:
          keys:
            - v1-git-{{ .Revision }}
            - v1-git-
      - checkout
      - save_cache:
          paths:
            - .git
          key: v1-git-{{ .Revision }}

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

      - run: sudo npm install -g greenkeeper-lockfile
      - run: npm --production=false install
      - run: greenkeeper-lockfile-update

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

      - run:
          name: Lint
          command: npm run test:lint -- --quiet --output-file test-results/eslint/results.xml --format junit
      
      - run:
          name: Unit
          environment:
              JEST_JUNIT_OUTPUT_DIR: test-results/unit
          command: npm run test:unit -- --reporters="default" --reporters="jest-junit" --coverage --coverageReporters=text --coverageReporters=lcov
      - store_artifacts:
          path: coverage
      
      - run:
          name: Build
          command: npm run build
      - store_artifacts:
          path: build
      - store_artifacts:
          path: dist
      - save_cache:
          paths:
            - build
          key: v1-build-{{ .Revision }}
      - save_cache:
          paths:
            - dist
          key: v1-dist-{{ .Revision }}
      
      
      - run:
          name: Integration
          environment:
              JEST_JUNIT_OUTPUT_DIR: test-results/integration
          command: npm run test:integration -- --reporters="default" --reporters="jest-junit"
      - store_test_results:
          path: test-results

      - run: greenkeeper-lockfile-upload
  deploy-npm:
    docker:
      - image: circleci/node:8.16-browsers
    working_directory: ~/repo
    steps:
      - run: |
          echo export RELEASE_VERSION="0.1.0-prerelease.$(date +'%Y%m%d%H%M%S')" >> $BASH_ENV
          echo export NPM_TAG=latest >> $BASH_ENV
          if [ "$CIRCLE_BRANCH" == "master" ]
            then echo export NPM_TAG=circlestable >> $BASH_ENV
          fi
          if [[ "$CIRCLE_BRANCH" == hotfix/* ]] # double brackets are important for matching the wildcard
            then echo export NPM_TAG=circlehotfix >> $BASH_ENV
          fi
      - restore_cache:
          keys:
            - v1-git-{{ .Revision }}
            - v1-git-
      - checkout
      - restore_cache:
          keys:
            - v1-dist-{{ .Revision }}
      - run: npm version --no-git-tag-version $RELEASE_VERSION
      - run: echo git tag $RELEASE_VERSION
      - run: echo git push $CIRCLE_REPOSITORY_URL $RELEASE_VERSION
      - run: |
          npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN
          npm publish --dry-run

deploy-requirements: &deploy-requirements
  requires:
    - build
  filters:
    branches:
      only:
        - master
        - develop
        - smoke
        - circle
        - /^hotfix\/.*/
      
workflows:
  version: 2
  build_and_deploy:
    jobs:
      - build
      - deploy-npm:
          <<: *deploy-requirements
