<commit_before>'use strict';

var http = require('http')
  , os = require('os')
  , spawn = require('child_process').spawn
  , tinyhttp = require('./tinyhttp')
  , open = require('./open')
  , watch = require('./watch')
  , reload = require('./reload')
  , server
  , watcher
  , reloader
  , args
  , port
  , appName
  , watchExp
  , watchArg
  , useWatch;

args = process.argv.slice(2);
port = !!(parseInt(args[0], 10)) ?  parseInt(args[0], 10) : 8000;
appName =  args[1] || args[0];

// check if we want to enable file watching
watchExp = /\-{2}live/g;
watchArg = args.join('').replace(/[^-]*/, '');
useWatch = !!watchExp.exec(watchArg);

function startServer(port) {

  server = tinyhttp().listen(port);
  
  server.on('close', function () {
    console.log('bye bye!');
  });
  
  server.on('error', function (err) {
    console.log('server error', err);
    if (err.code === 'EACCES' || err.code === 'EADDRINUSE') {
      console.log(os.EOL, '... Failed, starting server on port %d', port);
      console.log('... Retrying', os.EOL);

      port = (port < 3000) ? 3000 : port += 1;
      startServer(port);
      return;
    }
  });

  console.log(os.EOL + '... Starting Http Server on port %d', port);
  console.log('  [ctrl-c] -> exit.', os.EOL);

  // only runs once?
  open('http://localhost:' + port, appName);
}

function startWatching(path) {
  watcher = watch(path);
  reloader = reload();

  watcher.on('change', function (data) {
    if (data.type === 'create' || data.type === 'update') {
      reloader.notify(data.relPath);
    }
  });
}

process.on('uncaughtException', function (err) {
  console.log('%s ERROR: %s', os.EOL, err);
});

process.on('SIGINT', function () {
  server.stop();
  // TODO: also kill watch and livereload server
  // in case exiting the main process won't kill them.
  process.exit(1);
});


startServer(port);
if (useWatch) {
  startWatching(process.cwd());
}

<commit_msg>normalize cli, use gaze file watcher<commit_after>'use strict';

var http = require('http')
  , os = require('os')
  , spawn = require('child_process').spawn
  , tinyhttp = require('./tinyhttp')
  , open = require('./open')
  , watch = require('gaze')
  , reload = require('./reload')
  , args = process.argv.slice(2)
  , server = tinyhttp()
  , port = 8000
  , live = false
  , browser
  , reloader;

// parse options
while (args.length > 0) {
  var arg = args.shift();
  switch (arg) {
    case '--live':
    case '-l':
      live = true;
      break;
    case '--port':
    case '-p':
      var p = parseInt(args.shift(), 10);
      if (p) {
        port = p;
      }
      break;
    case '--browser':
    case '-b':
      browser = args.shift();
      break;
  }
}

if (live) {
  reloader = reload();
}

process.on('uncaughtException', function (err) {
  console.log('%s ERROR: %s', os.EOL, err);
});

process.on('SIGINT', function () {
  server.stop();
  if (live) {
    reloader.stop();
  }
  process.exit(1);
});

server.on('close', function () {
  console.log('bye bye!');
});

server.on('error', function (err) {
  console.log('server error', err);
  if (err.code === 'EACCES' || err.code === 'EADDRINUSE') {
    console.log(os.EOL, '... Failed, starting server on port %d', port);
    console.log('... Retrying', os.EOL);

    port = (port < 3000) ? 3000 : port += 1;
    server.listen(port);
  }
});

server.on('listening', function () {
  console.log(os.EOL + '... Starting Http Server on port %d', port);
  console.log('  [ctrl-c] -> exit.', os.EOL);

  open('http://localhost:' + port, browser);
});

server.listen(port);

if (live) {
  reloader.start();
  watch('*.html', function (err, watcher) {
    watcher.on('changed', function (filepath) {
      reloader.notify(filepath);
    });
  });
}
{'fraction_non_alphanumeric': 0.20536193029490615, 'fraction_numerical': 0.011260053619302948, 'mean_word_length': 2.9946466809421843, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4390864', 'n_tokens_mistral': 1359, 'n_tokens_neox': 1311, 'n_words': 304}