defaults: &defaults
  working_directory: ~/app
  docker:
    - image: circleci/node:8.11.2

version: 2

jobs:
  checkout_code:
    <<: *defaults
    steps:
      - checkout
      - save_cache:
          key: checkout-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
          paths:
            - ~/app

  bundle_dependencies:
    <<: *defaults
    steps:
      - restore_cache:
          keys:
            - checkout-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          keys:
            - package-cache-{{ checksum "package.json" }}
      - run: npm install
      - save_cache:
          key: package-cache-{{ checksum "package.json" }}
          paths:
            - ~/app/node_modules

  test:
      <<: *defaults
      steps:
        - restore_cache:
            keys:
              - checkout-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
        - restore_cache:
            keys:
              - package-cache-{{ checksum "package.json" }}
        - run: npm test

  build_dist:
    <<: *defaults
    steps:
      - restore_cache:
          keys:
            - checkout-cache-{{ .Branch }}-{{ .Environment.CIRCLE_SHA1 }}
      - restore_cache:
          keys:
            - package-cache-{{ checksum "package.json" }}

      - run:
          name: Compile the build
          command: |
            npm run build

      - run:
          name: Make version file
          command: |
            if [ $CIRCLE_TAG ]; then echo $CIRCLE_TAG-$CIRCLE_SHA1 > ./_version.txt; fi
      - run:
          name: Remove dev dependencies
          command: |
            rm -Rf ./node_modules
            npm install --production
      - run:
          name: Zip the Build
          command: |
            tar -cf build.tar --exclude='*.map' --exclude='test' --exclude='.git' --directory="./dist" .
            tar --append --file=build.tar ./config --exclude='*.map'
            tar --append --file=build.tar ./package.json
            tar --append --file=build.tar ./newrelic.js
            if [ $CIRCLE_TAG ]; then tar --append --file=build.tar ./_version.txt; fi
            tar --append --file=build.tar ./node_modules --exclude='*.map' --exclude='test' --exclude='.git'
            gzip build.tar
      - store_artifacts:
          path: build.tar.gz

      - persist_to_workspace:
          root: ./
          paths:
            - build.tar.gz

  release:
    machine:
      enabled: true
    working_directory: ~/app
    steps:
      - attach_workspace:
          at: ./
      - run:
          name: Publish release in Github
          command: |
            wget https://github.com/tcnksm/ghr/releases/download/v0.5.2/ghr_v0.5.2_linux_amd64.zip && unzip ghr_v0.5.2_linux_amd64.zip
            ./ghr -t $GITHUB_TOKEN -u $CIRCLE_PROJECT_USERNAME -r $CIRCLE_PROJECT_REPONAME $CIRCLE_TAG  ~/app/build.tar.gz

workflows:
  version: 2
  build-n-deploy:
    jobs:
      - checkout_code:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - bundle_dependencies:
          requires:
            - checkout_code
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - test:
          requires:
            - checkout_code
            - bundle_dependencies
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - build_dist:
          requires:
            - checkout_code
            - bundle_dependencies
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
      - release:
          requires:
            - test
            - build_dist
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/