{"size": 2129, "ext": "js", "max_stars_repo_path": "packages/tools/lib/npm/uninstall.js", "max_stars_repo_name": "wdavidw/node-mecano", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/tools/lib/npm/uninstall.js", "max_issues_repo_name": "wdavidw/node-mecano", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/tools/lib/npm/uninstall.js", "max_forks_repo_name": "wdavidw/node-mecano", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.29, "max_line_length": 94, "alphanum_fraction": 0.5659934241}
// Generated by CoffeeScript 2.7.0
  // # `nikita.tools.npm.uninstall`

// Remove one or more NodeJS packages.

// ## Example

// The following action uninstalls the coffescript package globally.

// ```js
  // const {$status} = await nikita.tools.npm.uninstall({
  //   name: 'coffeescript',
  //   global: true
  // })
  // console.info(`Package was uninstalled: ${$status}`)
  // ```

// ## Schema definitions
var definitions, handler,
  indexOf = [].indexOf;

definitions = {
  config: {
    type: 'object',
    properties: {
      'cwd': {
        $ref: 'module://@nikitajs/core/lib/actions/execute#/definitions/config/properties/cwd'
      },
      'name': {
        type: 'array',
        items: {
          type: 'string'
        },
        description: `Name of the package(s) to remove.`
      },
      'global': {
        type: 'boolean',
        default: false,
        description: `Uninstalls the current package context as a global package.`
      }
    },
    required: ['name'],
    if: {
      properties: {
        'global': {
          const: false
        }
      }
    },
    then: {
      required: ['cwd']
    }
  }
};

// ## Handler
handler = async function({
    config,
    tools: {log}
  }) {
  var global, installed, pkgs, stdout, uninstall;
  global = config.global ? '-g' : '';
  // Get installed packages
  installed = [];
  ({stdout} = (await this.execute({
    $shy: true,
    command: `npm list --json ${global}`,
    code: [0, 1],
    cwd: config.cwd,
    stdout_log: false
  })));
  pkgs = JSON.parse(stdout);
  if (Object.keys(pkgs).length) {
    installed = Object.keys(pkgs.dependencies);
  }
  // Uninstall
  uninstall = config.name.filter(function(pkg) {
    return indexOf.call(installed, pkg) >= 0;
  });
  if (!uninstall.length) {
    return;
  }
  await this.execute({
    command: `npm uninstall ${global} ${uninstall.join(' ')}`,
    cwd: config.cwd
  });
  return log({
    message: `NPM uninstalled packages: ${uninstall.join(', ')}`
  });
};

// ## Exports
module.exports = {
  handler: handler,
  metadata: {
    argument_to_config: 'name',
    definitions: definitions
  }
};
