CleverStack/cleverstack-cli

View on GitHub
bin/clever-remove

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

var path    = require('path')
  , lib     = GLOBAL.lib = require(path.join(__dirname, '..', 'lib'))
  , program = GLOBAL.program = require('commander')
  , Promise = require('bluebird')
  , rimraf  = require('rimraf')
  , fs      = require('fs')
  , async   = require('async');

/**  Define CLI Usage
================================*/
program.version(lib.pkg.version)
  .usage('[options] [modules ...]');

/**  Define CLI Help
================================*/
program.on('--help', function() {
  console.log('  Examples:');
  console.log('    clever remove clever-background-tasks');
  console.log('    clever remove auth clever-background-tasks');
  console.log('');
});

/**  Parse CLI Arguments
================================*/
program.parse(process.argv);
if (program.args.length < 1) {
  program.help();
}

// Tell promise we want long stack traces for easier debugging
Promise.longStackTraces();

/**
 * Finds all of the modules within lib.util.locations.get()
 *
 * @param  {String[]} modules
 * @return {Promise}
 * @api private
 */

function findModules(modules) {
  return new Promise(function(resolve, reject) {
    lib .util
      .locations.get()
      .then(function(locations) {
        var _modules = [];

        async.each(
          modules,
          function(module, next) {
            async.each(
              locations,
              function(location, fn) {
                var walk = require('findit')(path.join(location.moduleDir, location.modulePath))
                  , pkg;

                walk.on('file', function(file) {
                  if (path.basename(file) === 'bower.json') {
                    pkg = require(file);
                  } else if (path.basename(file) === 'package.json' && !fs.existsSync(file.replace('package.json', 'bower.json'))) {
                    pkg = require(file);
                  }

                  if (typeof pkg !== 'undefined' && pkg.name === module) {
                    var name = path.join(location.moduleDir, location.modulePath, pkg.rename || pkg.name);
                    if (_modules.indexOf(name) === -1) {
                      _modules.push(name);
                    }
                  }
                });

                walk.on('end', function() {
                  fn();
                });
              },
              next
           );
          },
          function(err) {
            if (!!err || _modules.length < 1) {
              return reject('There are no modules to remove.');
            }

            async.each(
              _modules,
              rimraf,
              function(err) {
                if (!!err) {
                  return reject(err);
                }

                resolve();
              }
           );
          });
      });
  });
}

/**  Start Removing
================================*/
findModules(program.args)
  .then(function() {
    lib.utils.success('Modules have been removed from your project.');
    process.exit(0);
  }, lib.utils.error);