version: 2
jobs:
  build:
    docker:
      - image: circleci/node:10
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-npm-deps-{{ checksum "package-lock.json" }}
      - run:
          name: Installing npm dependencies
          command: npm install
      - run:
          name: Setting up deployment environment
          command: |
            echo "Setting up git user"
            git config --global user.email circleci@circleci && git config --global user.name CircleCI

            echo "Logging in to npm"
            echo "$NPM_TOKEN" > ~/.npmrc
      - run:
          name: Handling package-lock changes
          command: |
            if git diff --exit-code package-lock.json; then
              echo "package-lock did not change"
            else
              if [[ $CIRCLE_BRANCH = *"greenkeeper/"* ]]; then
                echo "Committing package-lock changes"
                git add package-lock.json
                git commit -m 'chore(package): Update lockfile [ci skip]';
              else
                echo "Resetting package-lock changes"
                git checkout -- package-lock.json
              fi
            fi

            echo "Done"
      - save_cache:
          key: v1-npm-deps-{{ checksum "package-lock.json" }}
          paths:
            - node_modules

      # 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
      - run:
          name: Creating API documentation
          command: npm run docs

      # Test
      - run:
          name: Linting source files
          command: npm run lint -- --format junit --output-file ~/reports/eslint.xml
          when: always
      - run:
          name: Running tests
          command: node_modules/.bin/nyc npm test -- --reporter mocha-circleci-reporter
          environment:
            NODE_ENV: test
            MOCHA_FILE: ../reports/mocha.xml
          when: always
      - run:
          name: Checking documentation for broken links
          command: npm run test:docs
          when: always

      # Coverage checks
      - run:
          name: Checking test coverage
          command: node_modules/.bin/nyc check-coverage --statements 90 --branches 90 --functions 90 --lines 90
          when: always
      - run:
          name: Reporting test coverage
          command: node_modules/.bin/nyc report --reporter=lcov > coverage.lcov | node_modules/.bin/codecov
      - run:
          name: Check documentation coverage
          command: npm run docs:coverage
          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: git push origin $CIRCLE_BRANCH
      - deploy:
          name: Deploy changes
          command: |
            npx bump-version release --release-files out docs/api --gh-token $RELEASE_GITHUB_TOKEN

            if [ $CIRCLE_BRANCH = 'master' ]; then
              echo "Update GitHub pages"
              version=$(node -e "console.log(require('./package.json').version)")
              git clone git@github.com:$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME.git -b gh-pages ../pages
              cp -r docs/api/* ../pages
              cd ../pages
              git add .
              git commit -m "docs(gh-pages): Update for release v$version [ci skip]"
              git push origin gh-pages
              cd ../project
            fi
