CleverStack/cleverstack-cli

View on GitHub
bin/clever-build

Summary

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

var path     = require('path')
  , fs       = require('fs')
  , spawn    = require('child_process').spawn
  , readline = require('readline')
  , async    = require('async')
  , lib      = GLOBAL.lib     = require(path.join(__dirname, '..', 'lib'))
  , program  = GLOBAL.program = require('commander');


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


/**  Define CLI Help
================================*/
program.on('--help', function() {
  console.log('  Example:');
  console.log('    clever build');
  console.log('');
});


/**  Parse CLI Arguments
================================*/
program.parse(process.argv);


/**  Find Buildable Projects
================================*/
var currentDir = path.resolve(process.cwd())
  , files      = fs.readdirSync(currentDir)
  , folders    = [];

files
  .filter(function findBuildableProjects(d) {
    return d === 'package.json' || fs.statSync(path.resolve(path.join(currentDir, d))).isDirectory();
  })
  .forEach(function buildCompatibleProjects(d) {
    var isProject = d.indexOf('package.json') === -1
      , pkg       = path.resolve(path.join(currentDir, d, isProject ? 'package.json' : ''));

    if (fs.existsSync(pkg)) {
      var readPkg    = require(pkg)
        , hasPkgName = readPkg.hasOwnProperty('name');

      if (hasPkgName && readPkg.name.indexOf('package.json') === -1 && readPkg.name.indexOf('node-seed') === -1) {
        folders.push({
          path: path.resolve(path.join(pkg, '..'))
        });
      }
    }
  });

if (folders.length < 1) {
  lib.utils.fail('CleverStack couldn\'t find the frontend seed directory within ' + process.cwd());
}

/**  Start Building
================================*/
async.each(
  folders,
  function run(folder, next) {
    lib
    .util
    .grunt
    .runTask(
      folder.path,  'build',
      '--base',      folder.path,
      '--gruntfile', path.resolve(path.join(folder.path, 'Gruntfile.js'))
    )
    .then(function() {
      lib.utils.success('Build for ' + folder.path + ' was successful');
      next(null);
    })
    .catch(next);
  },
  function handleErr(err) {
    if (!!err) {
      lib.utils.fail(err);
    }

    process.exit(0);
  }
);