CleverStack/cleverstack-cli

View on GitHub
bin/clever-downgrade

Summary

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

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

/**  Define CLI Usage
================================*/
program
  .option('-v, --verbose', 'verbose output useful for debugging')
  .version(lib.pkg.version);

/**  Define CLI Help
================================*/
program.on('--help', function() {
  console.log('  Examples:');
  console.log('');
  console.log('    clever downgrade clever-orm');
  console.log('    clever downgrade clever-orm@0.0.1 clever-datatables@0.0.1');
  console.log('    clever downgrade backend');
  console.log('    clever downgrade frontend');
  console.log('');
});

/**  Parse CLI Arguments
================================*/
program.parse(process.argv);
var args = [];
process.argv.slice(2).forEach(function(arg) {
  if (arg !== '-v' && arg !== '--verbose') {
    args.push(arg);
  }
});

/**  Start Downgrading
================================*/
// We don't need version numbers when we're checking for backend or frontend...
var _args = args.map(function(a) {
  return a.split('@')[0];
});

// Figure out what we're trying to upgrade...
var findBackend = _args.indexOf('backend');
if (findBackend > -1) {
  var backendVersion = args[findBackend].split('@')[1];
  args[findBackend] = 'node-seed' + (typeof backendVersion !== 'undefined' ? '@' + backendVersion : '');
}

var findFrontend = _args.indexOf('frontend');
if (findFrontend > -1) {
  var frontendVersion = args[findFrontend].split('@')[1];
  args[findFrontend] = 'angular-seed' + (typeof frontendVersion !== 'undefined' ? '@' + frontendVersion : '');
}

var modules = [];
if (args.length < 1) {
  modules.push('node-seed');
  modules.push('angular-seed');
} else {
  modules = args;
}

lib.util.locations.get()
.then (function (locations) {
  // Filter for module's existence
  async.map(modules, function (module, filter) {
    var moduleSplit   = module.split('@')
      , moduleName    = moduleSplit[0]
      , moduleVersion = '*';

    if (typeof moduleSplit[1] !== 'undefined') {
      moduleVersion = moduleSplit[1];
    }

    lib
      .util.module
      .findConfigAndVersionForModule(locations, moduleName, moduleVersion, 'lt')
      .then(function (m) {
        filter(null, m);
      }, function(err) {
        filter(err);
      });
  },
  function(err, modulesToInstall) {
    var modulesInstall = modulesToInstall.filter(function(m) {
      return typeof m === 'object' && m !== null;
    });

    if (!modulesInstall.length) {
      return lib.utils.fail('There are no modules to downgrade.');
    }

    var backendModules = modulesInstall.filter(function(m) {
      return m.type === 'backend';
    })
    .map(function(m) {
      return m.name;
    });

    var frontendModules = modulesInstall.filter(function(m) {
      return m.type === 'frontend';
    })
    .map(function(m) {
      return m.name;
    });

    var actions     = []
      , backendLoc  = _.find(locations, function(loc) { return loc.name === 'backend'; })
      , frontendLoc = _.find(locations, function(loc) { return loc.name === 'frontend'; });

    if (backendModules.length && !!backendLoc) {
      actions.push(lib.project.setupModules(backendLoc, backendModules));
    }

    if (frontendModules.length && !!frontendLoc) {
      actions.push(lib.project.setupModules(frontendLoc, frontendModules));
    }

    Promise
      .all(actions)
      .then(function() {
        lib.utils.success('Downgrade process has been completed.');
        process.exit(0);
      })
      .catch(function(err) {
        lib.utils.fail(err);
      });
  });
})
.catch(function(err) {
  lib.utils.fail(err);
});