version: 2
jobs:
  build:
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-npm-deps-{{ checksum "package-lock.json" }}
      - run:
          name: Installing npm dependencies
          command: npm install
      - run:
          name: Resetting package-lock changes
          command: git checkout -- package-lock.json
      - save_cache:
          key: v1-npm-deps-{{ checksum "package-lock.json" }}
          paths:
            - node_modules
      - run:
          name: Preparing lockfile update
          command: $(npm bin)/greenkeeper-lockfile-update

      # Code Quality checks
      # TODO: Lint commit message
      # with git log --format=%B -n 1 $CIRCLE_SHA1 | npx commitlint

      # Build
      - run:
          name: Transpiling source code
          command: npm run compile

      # Test
      - run:
          name: Linting source files
          command: npm run lint -- --format junit --output-file ~/reports/eslint.xml
          when: always

      # Store Results
      - store_test_results:
          path: ~/reports
          when: always
      - store_artifacts:
          path: ~/reports
          when: always

      # Deployment
      - add_ssh_keys
      - deploy:
          name: Updating lockfile
          command: $(npm bin)/greenkeeper-lockfile-upload
      - deploy:
          name: Deploy changes
          command: |
            echo "$NPM_TOKEN" > ~/.npmrc

            git config --global user.email circleci@circleci && git config --global user.name CircleCI && git remote --v

            node out/cli.js release --gh-token $GITHUB_TOKEN --verbose

