CleverStack/cleverstack-cli

View on GitHub
bin/clever

Summary

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

var path              = require('path')
  , shell             = require('shelljs')
  , async             = require('async')
  , configStore       = require('configstore')
  , rootDir           = path.resolve(path.join(__dirname, '..'))
  , libDir            = path.join(rootDir, 'lib')
  , lib               = require(path.join(libDir, 'index'))
  , updateNotifier    = require('update-notifier')
  , checkUpdatesEvery = 1000 * 60 * 60 * 24 // once every day
  , packageJson       = require(path.join(rootDir, 'package.json'))
  , notifier;

async.waterfall(
  [
    function checkDependencies(checksDone) {
      async.each(
        [
          { cmd: 'npm',   name: 'NPM',        install: '.Please install Node.JS before proceeding.' },
          { cmd: 'bower', name: 'Bower',      install: ' please type: npm install -g bower' },
          { cmd: 'grunt', name: 'Grunt-CLI',  install: ' please type: npm install -g grunt-cli' }
        ],
        function checkDependencies(check, checkDone) {
          checkDone(!!shell.which(check.cmd) ? null : check.name + ' is required before using CleverStack-CLI' + check.install);
        },
        checksDone
     );
    },

    function checkForUpdates(checkDone) {
      var updateConfig = new configStore('update-notifier-cleverstack-cli', {
        optOut          : false,
        lastUpdateCheck : Date.now()
      });

      // Only check for updates on a set interval
      if (!process.env.HTTP_PROXY && !process.env.HTTPS_PROXY && Date.now() - updateConfig.get('lastUpdateCheck') > checkUpdatesEvery) {
        notifier = updateNotifier({ pkg: packageJson, callback: checkDone });
      } else {
        checkDone(null);
      }
    }
  ],
  function runCommand(err, update) {
    var hasErrored = err !== undefined && err !== null;
    if (!!hasErrored && !update && err.code !== 'ENOTFOUND') {
      lib.utils.error(err);
    } else {
      if (!!hasErrored && !!update && update.type !== 'latest') {
        notifier.update = update;
        notifier.notify({ defer: true });
      }

      // avoid the command within process.argv
      lib.command(__dirname, process.argv[ 2 ], process.argv.slice(3));
    }
  }
);