{"size": 1719, "ext": "js", "max_stars_repo_path": "src/tasks/remove.js", "max_stars_repo_name": "BrianLusina/drone", "max_stars_repo_stars_event_min_datetime": "2021-05-08T12:33:58.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-08T12:33:58.000Z", "max_issues_repo_path": "src/tasks/remove.js", "max_issues_repo_name": "BrianLusina/drone", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/tasks/remove.js", "max_forks_repo_name": "BrianLusina/drone", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.7258064516, "max_line_length": 88, "alphanum_fraction": 0.6364165212}
#!/usr/bin/env node
'use strict';

const Chalk = require('chalk');
const log = console.log;
const hive = require('../handlers/hive');
const QuickSearch = require('../handlers/quick_search');
const Log = require('../utils/log_utils');
const Utils = require('../utils/utils');
const GradleUtils = require('../utils/gradle_utils');
const Constants = require('../utils/constants');

function findAndDelete(dep, gradleFilePath) {
  const found = Utils.findStringInFileSync(dep.dependency, gradleFilePath);

  // Found something
  if (found.length !== 0) {
    const toDelete = found[0];
    
    Log.title(`Deleted ${Chalk.green(dep.dependency)}`);
    GradleUtils.deleteLineFromFile(gradleFilePath, toDelete.line-1);
  } else {
    Log.titleError(`Couldn't find ${Chalk.green(dep.dependency)} in this build.gradle`);
  }
}

function remove(module, dependencies, gradleFilePath) {
  if (GradleUtils.gradleFileExistsIn(module)) {
    dependencies.forEach(dep => {
      findAndDelete(dep, gradleFilePath)
    })
  } else {
    Log.titleError(`Can't find this gradle file`)
    log(gradleFilePath)
  }
}

// Main code //
const self = module.exports = {
  init: (input) => {
    if (input.length < 3) {
      Log.titleError(`You need to specify the module`);
      return;
    }

    const pair = input[1];
    const module = input[2];

    QuickSearch.search(pair)
      .then(result => {
        if (result.rating === 1) {
          hive.getWithVersions(result.target)
            .then(info => {
              remove(module, info.dependencies, GradleUtils.gradleFilePath(module));
            });
        } else {
          Log.titleError(`Can't find ${Chalk.yellow(pair)} in this build.gradle`);
        }
      });
  }
};
