<commit_before>/**
* The Docvy Plugin Installer
*
* The MIT License (MIT)
* Copyright (c) 2015 GochoMugo <<EMAIL>>
*/


// built-in modules
var fs = require("fs");
var os = require("os");
var npm = require("npm");
var path = require("path");


// npm-installed modules
var lodash = require("lodash");
var ncp = require("ncp");
var rimraf = require("rimraf");
var utils = require("docvy-utils");


// own modules
var errors = require("./errors");


// module variables
var pluginsDir = utils.getPath("app.plugins");
var tmpDir = path.join(os.tmpdir(), "tmp_docvy_plugins");


/**
* Install plugins using NPM
*
* @param <packages> -- {String|Array} name of packages
* @param [callback] -- {Function} callback(err)
*/
exports.install = npmInstall;
exports.npmInstall = npmInstall;
function npmInstall(packages, callback) {
  callback = utils.defineCallback(callback);
  if (lodash.isString(packages)) { packages = [packages]; }
  npm.load({
    loglevel: "silent",
    prefix: tmpDir
  }, function(err) {
    if (err) { return callback(new errors.NpmLoadError(err)); }
    npm.commands.install(packages, function(err) {
      if (err) { return callback(new errors.NpmInstallError(err)); }
      // ensure pluginsDir exists
      fs.mkdir(pluginsDir, function() {
        ncp(tmpDir + "/node_modules/", pluginsDir, function(err) {
          if (err) {
            return callback(new errors.
              PluginsMoveFromTempError(err));
          }
          return callback(null);
        }); // ncp
      }); // fs.mkdir
    }); // npm.commands.install
  }); // npm.load
}


/**
* Listing installed plugins
*
* @param [callback] -- {Function} callback(err, plugins)
*/
exports.listPlugins = listPlugins;
function listPlugins(callback) {
  callback = utils.defineCallback(callback);
  fs.readdir(pluginsDir, function(err, files) {
    if (err) { return callback(new errors.PluginsListingError(err)); }
    return callback(null, files);
  });
}


/**
* Uninstall plugin(s)<commit_msg>No need to ensure plugins dir exists<commit_after>/**
* The Docvy Plugin Installer
*
* The MIT License (MIT)
* Copyright (c) 2015 GochoMugo <<EMAIL>>
*/


// built-in modules
var fs = require("fs");
var os = require("os");
var npm = require("npm");
var path = require("path");


// npm-installed modules
var lodash = require("lodash");
var ncp = require("ncp");
var rimraf = require("rimraf");
var utils = require("docvy-utils");


// own modules
var errors = require("./errors");


// module variables
var pluginsDir = utils.getPath("app.plugins");
var tmpDir = path.join(os.tmpdir(), "tmp_docvy_plugins");


/**
* Install plugins using NPM
*
* @param <packages> -- {String|Array} name of packages
* @param [callback] -- {Function} callback(err)
*/
exports.install = npmInstall;
exports.npmInstall = npmInstall;
function npmInstall(packages, callback) {
  callback = utils.defineCallback(callback);
  if (lodash.isString(packages)) { packages = [packages]; }
  npm.load({
    loglevel: "silent",
    prefix: tmpDir
  }, function(err) {
    if (err) { return callback(new errors.NpmLoadError(err)); }
    npm.commands.install(packages, function(err) {
      if (err) { return callback(new errors.NpmInstallError(err)); }
      ncp(tmpDir + "/node_modules/", pluginsDir, function(err) {
        if (err) {
          return callback(new errors.
            PluginsMoveFromTempError(err));
        }
        return callback(null);
      }); // ncp
    }); // npm.commands.install
  }); // npm.load
}


/**
* Listing installed plugins
*
* @param [callback] -- {Function} callback(err, plugins)
*/
exports.listPlugins = listPlugins;
function listPlugins(callback) {
  callback = utils.defineCallback(callback);
  fs.readdir(pluginsDir, function(err, files) {
    if (err) { return callback(new errors.PluginsListingError(err)); }
    return callback(null, files);
  });
}


/**
* Uninstall plugin(s)
{'fraction_non_alphanumeric': 0.15896774193548388, 'fraction_numerical': 0.002064516129032258, 'mean_word_length': 3.664259927797834, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1151662', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1203, 'n_words': 297}