#!/usr/bin/env node


/**
 * Module dependencies
 */

var _ = require('lodash');
var async = require('async');
var reportback = require('reportback')();
var sailsgen = require('sails-generate');
var package = require('../package.json');
var rconf = require('../lib/app/configuration/rc');
var _generateAPI = require('./_generate-api');


/**
 * `sails generate`
 *
 * Generate module(s) for the app in our working directory.
 * Internally, uses ejs for rendering the various module templates.
 */

module.exports = function() {

  // Build initial scope
  var scope = {
    rootPath: process.cwd(),
    modules: {},
    sailsPackageJSON: package,
  };

  // Mix-in rc config
  _.merge(scope, rconf.generators);

  // TODO: just do a top-level merge and reference
  // `scope.generators.modules` as needed (simpler)
  _.merge(scope, rconf);


  // Pass the original CLI arguments down to the generator
  // (but first, remove commander's extra argument)
  // (also peel off the `generatorType` arg)
  var cliArguments = Array.prototype.slice.call(arguments);
  cliArguments.pop();
  scope.generatorType = cliArguments.shift();
  scope.args = cliArguments;

  // Create a new reportback
  var cb = reportback.extend();

  // Show usage if no generator type is defined
  if (!scope.generatorType) {
    return cb.log.error('Usage: sails generate [something]');
  }

  // Set the "invalid" exit to forward to "error"
  cb.error = function(msg) {
    var log = this.log || cb.log;
    log.error(msg);
    process.exit(1);
  };

  cb.invalid = 'error';

  // If the generator type is "api", we currently treat it as a special case.
  // (todo: pull this out into a simple generator)
  if (scope.generatorType === 'api') {
    if (scope.args.length === 0) {
      return cb.error('Usage: sails generate api [api name]');
    }
    _generateAPI(scope, cb);
  }

  // Otherwise just run whichever generator was requested.
  else {
    cb.success = function() {
      cb.log.info('Generated a new ' + scope.generatorType + ' `' + scope.id + '` at ' + scope.destDir + scope.globalID + '.js!');
    };

    //
    return sailsgen(scope, cb);
  }

};

@@ -70,24 +70,52 @@
 ('lodash');%0A
+var util = require('util');%0A
 var async = 
@@ -1977,136 +1977,491 @@
 ) %7B%0A
+%0A
       
-cb.log.info('Generated a new ' + scope.generatorType + ' %60' + scope.id + '%60 at ' + scope.destDir + scope.globalID + '.js!'
+// Determine the output path%0A      var outputPath = scope.outputPath;%0A      if (!outputPath && scope.filename) %7B%0A        outputPath = scope.destDir + scope.filename;%0A      %7D%0A      var humanizedPath;%0A      if (outputPath) %7B%0A        humanizedPath = 'at ' + outputPath;%0A      %7D%0A      else %7B%0A        humanizedPath = 'in ' + scope.destDir;%0A      %7D%0A%0A      cb.log.info(util.format(%0A        'Generated a new %25s (%22%25s%22) %25s!',%0A        scope.generatorType, scope.id, humanizedPath%0A      )
 );%0A 

Improve output