'use strict';
var _ = require('lodash');
var path = require('path');
var fs = require('fs');
var yargs = require('yargs');
var chalk = require('chalk');
var gutil = require('gulp-util');

var DEFAULT_TARGET = 'app';
var clientFolder = 'client';

var setClientFolder = exports.setClientFolder = function(folder) {
    clientFolder = folder;
};

var basenameToTarget = function(basename) {
    return basename === 'index' ? DEFAULT_TARGET : _(basename.split('-')).last();
};

var getAllTargets = exports.getAllTargets = function() {
    var re = /index(|-[^\.]+)\.html/;

    return _(fs.readdirSync(clientFolder))
        .filter(function(name) {
            return re.test(name);
        })
        .map(function(name) {
            var filename = path.basename(name, '.html');
            return basenameToTarget(filename);
        })
        .sortBy()
        .value();

};

var checkTargets = exports.checkTargets = function(args, opts) {
    if(!args.target) {
        return;
    }
    var badTargets = _.difference([].concat(args.target), getAllTargets());
    if(badTargets.length !== 0) {
        var message = 'The following targets were not found in the folder "' + clientFolder + '": ' + badTargets.join(',');
        gutil.log(chalk.red('(ERR) ' + message));

        throw new Error(message);
    }
};

exports.askForTargets = function(taskname) {
    taskname = taskname || '';
    return yargs.usage(chalk.cyan('Usage: $0 ' + taskname))
        .alias('t', 'target')
        .default('t', getAllTargets())
        .example(chalk.white('$0 ' + taskname), chalk.cyan('Run the gulp task for all available targets.'))
        .example(chalk.white('$0 ' + taskname + ' -t a'), chalk.cyan('Run the gulp task, only for target a.'))
        .example(chalk.white('$0 ' + taskname + ' -t a -t b --target c'), chalk.cyan('Run the gulp task, for targets a, b, and c.'))
        .describe('t', chalk.cyan('Run the gulp task for each of the specified distribution target[s]'))
        .check(checkTargets)
        .argv;

};
@@ -931,24 +931,41 @@
 gs, opts) %7B%0A
+    var message;%0A
     if(!args
@@ -971,24 +971,24 @@
 s.target) %7B%0A
-
         retu
@@ -1111,28 +1111,24 @@
 ) %7B%0A        
-var 
 message = 'T
@@ -1278,16 +1278,513 @@
 age));%0A%0A
+        throw new Error(message);%0A    %7D%0A    if(!args.mode) %7B%0A        return;%0A    %7D%0A%0A    if(!_.isString(args.mode)) %7B%0A        message = 'mode should be a string instead of: ' + args.mode.toString();%0A        gutil.log(chalk.red('(ERR) ' + message));%0A%0A        throw new Error(message);%0A    %7D%0A%0A    if(args.mode !== 'prod' && args.mode !== 'dev') %7B%0A        message = 'valid values for mode are %22prod%22 or %22dev%22,  instead got: ' + args.mode.toString();%0A        gutil.log(chalk.red('(ERR) ' + message));%0A%0A
         
@@ -2024,16 +2024,94 @@
 gets())%0A
+        .alias('m', 'mode')%0A        .string('m')%0A        .default('m', 'dev')%0A
         
@@ -2450,24 +2450,160 @@
 , and c.'))%0A
+        .example(chalk.white('$0 ' + taskname + ' -t a -t b -m prod'), chalk.cyan('Run the gulp task, for targets a, b in prod mode.'))%0A
         .des
@@ -2691,24 +2691,116 @@
 arget%5Bs%5D'))%0A
+        .describe('m', chalk.cyan('Run the gulp task in the specified mode (prod or dev)'))%0A
         .che

Add mode options for yargs