var Command = require('./Command');
var _super = Command.prototype;
var child_process = require('child_process');
var npath = require('path');

module.exports = Command.extend({

  'initialize': function () {

    var self = this;
    _super.initialize.apply(self, arguments);

    self.ensureAsimovProject().done(function () {

      self.logAsimovHeader();
      self.logger.pending(self.namespace, 'Running tests for "' + self.options.meta.name +'" ' + self.options.meta.version);

      var commandIndex = self.options.args.indexOf('test');
      var grep = self.options.args[commandIndex + 1];

      var reporterFlagIndex = self.options.args.indexOf('-R');
      if (reporterFlagIndex === commandIndex + 1) grep = null;

      // var reporterFlagIndex = self.options.args.indexOf('-R');

      var path = npath.resolve(__dirname, '../../node_modules/mocha/bin/mocha');
      var paths = self.getTestPaths(null, grep, []);
      if (paths.length) {
        var child = child_process.fork(path, paths);
        child.on('exit', function (code) {
          self.filesystem.recursiveDelete(npath.join(process.cwd(), 'tests/temp'));
          process.exit(code);
        });
      }
    });
  },

  'getTestPaths': function (path, grep, paths) {

    var self = this;
    if (!grep) return ['tests/**/*.test.js'];

    paths = paths || [];
    path = path || npath.join(process.cwd(), 'tests');

    self.filesystem.readDirectory(path, function (subPath) {
      if (subPath.indexOf('.test.js') > 0 && subPath.toLowerCase().indexOf(grep.toLowerCase()) >= 0) {
        paths.push(subPath);
      }
      else if (self.filesystem.isDirectory(subPath)) {
        paths = self.getTestPaths(subPath, grep, paths);
      }
    });

    return paths;
  }
});
var Command = require('./Command');
var _super = Command.prototype;
var child_process = require('child_process');
var npath = require('path');

module.exports = Command.extend({

  'initialize': function () {

    var self = this;
    _super.initialize.apply(self, arguments);

    self.ensureAsimovProject().done(function () {

      self.logAsimovHeader();
      self.logger.pending(self.namespace, 'Running tests for "' + self.options.meta.name +'" ' + self.options.meta.version);

      var commandIndex = self.options.args.indexOf('test');
      var grep = self.options.args[commandIndex + 1];

      var reporterFlagIndex = self.options.args.indexOf('-R');
      if (reporterFlagIndex === commandIndex + 1) grep = null;
      var reporterFlag = ~reporterFlagIndex && self.options.args[reporterFlagIndex + 1];

      var path = npath.resolve(__dirname, '../../node_modules/mocha/bin/mocha');
      var flags = self.getTestPaths(null, grep, []);

      if (reporterFlag) {
        flags = flags.concat(['-R', reporterFlag]);
      }

      if (flags.length) {
        var child = child_process.fork(path, flags);
        child.on('exit', function (code) {
          self.filesystem.recursiveDelete(npath.join(process.cwd(), 'tests/temp'));
          process.exit(code);
        });
      }
    });
  },

  'getTestPaths': function (path, grep, paths) {

    var self = this;
    if (!grep) return ['tests/**/*.test.js'];

    paths = paths || [];
    path = path || npath.join(process.cwd(), 'tests');

    self.filesystem.readDirectory(path, function (subPath) {
      if (subPath.indexOf('.test.js') > 0 && subPath.toLowerCase().indexOf(grep.toLowerCase()) >= 0) {
        paths.push(subPath);
      }
      else if (self.filesystem.isDirectory(subPath)) {
        paths = self.getTestPaths(subPath, grep, paths);
      }
    });

    return paths;
  }
});
use different test reporters with -R [name]