<commit_before>var fs = require('fs'),
    path = require('path'),
    argv = require('optimist').argv,
    connect = require('connect'),
    finalhandler = require('finalhandler'),
    http = require('http'),
    serveStatic = require('serve-static'),
    tinylr = require('tiny-lr-fork'),
    lr = require('connect-livereload'),
    vfs = require('vinyl-fs'),
    request = require('request'),
    IonicProject = require('./project'),
    Task = require('./task').Task,
    proxyMiddleware = require('proxy-middleware'),
    url = require('url'),
    xml2js = require('xml2js'),
    IonicStats = require('./stats').IonicStats;

var DEFAULT_HTTP_PORT = 8100;
var DEFAULT_LIVE_RELOAD_PORT = 35729;
var IONIC_LAB_URL = '/ionic-lab';

var IonicTask = function() {};

IonicTask.prototype = new Task();

IonicTask.prototype.run = function(ionic) {
  var self = this;
  this.ionic = ionic;
  this.port = argv._[1];
  this.liveReloadPort = argv._[2];

  this.loadSettings(function(){

    if(self.isAddressCmd) {
      console.log( self.address );
      process.exit();
    }

    self.start(ionic);

    if(ionic.hasFailed) return;

    ionic.latestVersion.promise.then(function(){
      ionic.printVersionWarning();
    });
  });
};


IonicTask.prototype.loadSettings = function(cb) {
  var project = IonicProject.load();
  var self = this;

  this.port = this.port || argv.port || argv.p || DEFAULT_HTTP_PORT;
  this.liveReloadPort = this.liveReloadPort || argv.livereloadport || argv['livereload-port'] || argv.i || DEFAULT_LIVE_RELOAD_PORT;

  this.launchBrowser = !argv.nobrowser && !argv.b;
  this.launchLab = this.launchBrowser && argv.lab;
  this.runLivereload = !argv.nolivereload && !argv.r;
  this.useProxy = !argv.noproxy && !argv.x;
  this.proxies = project.get('proxies') || [];
  this.watchSass = project.get('sass') === true && !argv.nosass && !argv.n;
  this.gulpStartupTasks = project.get('gulpStartupTasks');
  this.watchPatterns = project.get('watchPatterns') || ['www/**/*', '!www/lib/**/*'];
  this.printConsoleLogs = argv.consolelogs || argv['console-logs'] || argv.c;
  this.printServerLogs = argv.serverlogs || argv['server-logs'] || argv.s;
  this.isAddressCmd = argv._[0].toLowerCase() == 'address';
  this.contentSrc = path.join('www', this.ionic.getContentSrc());

  this.getAddress(cb);

  process.stdin.on('readable', function() {
    var input = process.stdin.read();
    if (input === null) return;
    input = (input + '').trim();

    if(input == 'restart' || input == 'r') {
      self._goToUrl('/?restart=' + Math.floor((Math.random() * 899999) + 100000));

    } else if(input.indexOf('goto ') === 0 || input.indexOf('g ') === 0) {
      var url = input.replace('goto ', '').replace('g ', '');
      self._goToUrl(url);

    } else if(input == 'consolelogs' || input == 'c') {
      self.printConsoleLogs = !self.printConsoleLogs;
      console.log('Console log output: '.green + (self.printConsoleLogs ? 'enabled' : 'disabled'));
      self._goToUrl('/?restart=' + Math.floor((Math.random() * 899999) + 100000));

    } else if(input == 'serverlogs' || input == 's') {
      self.printServerLogs = !self.printServerLogs;
      console.log('Server log output: '.green + (self.printServerLogs ? 'enabled' : 'disabled'));

    } else if(input.match(/^go\([+\-]?[0-9]{1,9}\)$/)) {
      self._goToHistory(input);

    } else if(input == 'help' || input == 'h') {
      self.printCommandTips();

    } else if(input == 'quit' || input == 'q') {
      self.childProcess.kill('SIGTERM');
      process.exit();

    } else if(input == 'clear' || input == 'clr') {
      process.stdout.write("\u001b[2J\u001b[0;0H");

    } else {
      console.log('\nInvalid ionic server command'.error.bold);
      self.printCommandTips();
    }

  });
};


IonicTask.prototype.printCommandTips = function(ionic) {
  console.log('Ionic server commands, enter:'.green.bold);
  console.log('  restart' + ' or '.green + 'r' + ' to restart the client app from the root'.green);
  console.log('  goto' + ' or '.green + 'g' + ' and a url to have the app navigate to the given url'.green);
  console.log('  consolelogs' + ' or '.green + 'c' + ' to enable/disable console log output'.green);
  console.log('  serverlogs' + ' or '.green + 's' + ' to enable/disable server log output'.green);
  console.log('  quit' + ' or '.green + 'q' + ' to shutdown the server and exit'.green);
  console.log('');
};


IonicTask.prototype.start = function(ionic) {
  try {
    var self = this;
    var app = connect();
    var childProcess = null;

    if (!fs.existsSync( path.resolve('www') )) {
      return ionic.fail('"www" directory cannot be found. Please make sure the working directory is an Ionic project.');
    }

    // gulpStartupTasks should be an array of tasks set in the project config
    // watchSass is for backwards compatible sass: true project config
    if((this.gulpStartupTasks && this.gulpStartupTasks.length) || this.watchSass) {
      var tasks = this.gulpStartupTasks || ['sass','watch'];

      console.log('Gulp startup tasks:'.green.bold, tasks);
      require('cross-spawn').spawn('gulp', tasks, { stdio: 'inherit' });
    }<commit_msg>Including changes from zckrs, adjusting to previous fix for #186.<commit_after>var fs = require('fs'),
    path = require('path'),
    argv = require('optimist').argv,
    connect = require('connect'),
    finalhandler = require('finalhandler'),
    http = require('http'),
    serveStatic = require('serve-static'),
    tinylr = require('tiny-lr-fork'),
    lr = require('connect-livereload'),
    vfs = require('vinyl-fs'),
    request = require('request'),
    IonicProject = require('./project'),
    Task = require('./task').Task,
    proxyMiddleware = require('proxy-middleware'),
    url = require('url'),
    xml2js = require('xml2js'),
    IonicStats = require('./stats').IonicStats;

var DEFAULT_HTTP_PORT = 8100;
var DEFAULT_LIVE_RELOAD_PORT = 35729;
var IONIC_LAB_URL = '/ionic-lab';

var IonicTask = function() {};

IonicTask.prototype = new Task();

IonicTask.prototype.run = function(ionic) {
  var self = this;
  this.ionic = ionic;
  this.port = argv._[1];
  this.liveReloadPort = argv._[2];

  this.loadSettings(function(){

    if(self.isAddressCmd) {
      console.log( self.address );
      process.exit();
    }

    self.start(ionic);

    if(ionic.hasFailed) return;

    ionic.latestVersion.promise.then(function(){
      ionic.printVersionWarning();
    });
  });
};


IonicTask.prototype.loadSettings = function(cb) {
  var project = IonicProject.load();
  var self = this;

  this.port = this.port || argv.port || argv.p || DEFAULT_HTTP_PORT;
  this.liveReloadPort = this.liveReloadPort || argv.livereloadport || argv['livereload-port'] || argv.i || DEFAULT_LIVE_RELOAD_PORT;

  this.launchBrowser = !argv.nobrowser && !argv.b;
  this.launchLab = this.launchBrowser && argv.lab;
  this.runLivereload = !argv.nolivereload && !argv.r;
  this.useProxy = !argv.noproxy && !argv.x;
  this.proxies = project.get('proxies') || [];
  this.watchSass = project.get('sass') === true && !argv.nosass && !argv.n;
  this.gulpStartupTasks = project.get('gulpStartupTasks');
  this.watchPatterns = project.get('watchPatterns') || ['www/**/*', '!www/lib/**/*'];
  this.printConsoleLogs = argv.consolelogs || argv['console-logs'] || argv.c;
  this.printServerLogs = argv.serverlogs || argv['server-logs'] || argv.s;
  this.isAddressCmd = argv._[0].toLowerCase() == 'address';
  this.contentSrc = path.join('www', this.ionic.getContentSrc());

  this.getAddress(cb);

  process.stdin.on('readable', function() {
    var input = process.stdin.read();
    if (input === null) return;
    input = (input + '').trim();

    if(input == 'restart' || input == 'r') {
      self._goToUrl('/?restart=' + Math.floor((Math.random() * 899999) + 100000));

    } else if(input.indexOf('goto ') === 0 || input.indexOf('g ') === 0) {
      var url = input.replace('goto ', '').replace('g ', '');
      self._goToUrl(url);

    } else if(input == 'consolelogs' || input == 'c') {
      self.printConsoleLogs = !self.printConsoleLogs;
      console.log('Console log output: '.green + (self.printConsoleLogs ? 'enabled' : 'disabled'));
      self._goToUrl('/?restart=' + Math.floor((Math.random() * 899999) + 100000));

    } else if(input == 'serverlogs' || input == 's') {
      self.printServerLogs = !self.printServerLogs;
      console.log('Server log output: '.green + (self.printServerLogs ? 'enabled' : 'disabled'));

    } else if(input.match(/^go\([+\-]?[0-9]{1,9}\)$/)) {
      self._goToHistory(input);

    } else if(input == 'help' || input == 'h') {
      self.printCommandTips();

    } else if(input == 'quit' || input == 'q') {
      self.childProcess.kill('SIGTERM');
      process.exit();

    } else if(input == 'clear' || input == 'clr') {
      process.stdout.write("\u001b[2J\u001b[0;0H");

    } else {
      console.log('\nInvalid ionic server command'.error.bold);
      self.printCommandTips();
    }

  });
};


IonicTask.prototype.printCommandTips = function(ionic) {
  console.log('Ionic server commands, enter:'.green.bold);
  console.log('  restart' + ' or '.green + 'r' + ' to restart the client app from the root'.green);
  console.log('  goto' + ' or '.green + 'g' + ' and a url to have the app navigate to the given url'.green);
  console.log('  consolelogs' + ' or '.green + 'c' + ' to enable/disable console log output'.green);
  console.log('  serverlogs' + ' or '.green + 's' + ' to enable/disable server log output'.green);
  console.log('  quit' + ' or '.green + 'q' + ' to shutdown the server and exit'.green);
  console.log('');
};


IonicTask.prototype.start = function(ionic) {
  try {
    var self = this;
    var app = connect();
    var childProcess = null;

    if (!fs.existsSync( path.resolve('www') )) {
      return ionic.fail('"www" directory cannot be found. Please make sure the working directory is an Ionic project.');
    }

    // gulpStartupTasks should be an array of tasks set in the project config
    // watchSass is for backwards compatible sass: true project config
    if((this.gulpStartupTasks && this.gulpStartupTasks.length) || this.watchSass) {
      var tasks = this.gulpStartupTasks || ['sass','watch'];

      console.log('Gulp startup tasks:'.green.bold, tasks);
      self.childProcess = require('cross-spawn').spawn('gulp', tasks, { stdio: 'inherit' });
    }
{'fraction_non_alphanumeric': 0.1823869104908566, 'fraction_numerical': 0.010490856592877768, 'mean_word_length': 3.5375545851528383, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6049879', 'n_tokens_mistral': 3538, 'n_tokens_neox': 3366, 'n_words': 722}