<commit_before>var port = Number(process.env.PORT)  || 8080;

var http = require('http'),
    path = require('path'),
    parseURL = require('url').parse,
    spawn = require('child_process').spawn;

var lodashCli = require('lodash-cli/package.json').bin.lodash;
lodashCli = require.resolve('lodash-cli/' + lodashCli);

var ecstatic = require('ecstatic');

var mount = ecstatic({ root: path.join(__dirname, '/public'), cache: 3600, showDir: false });

http.createServer(function(req, res) {
  var parsedURL = parseURL(req.url, true);
  if (parsedURL.pathname == '/build' && parsedURL.query) {
    buildLodash(req, res, parsedURL.query);
  } else {
    mount(req, res);
  }
}).listen(port);

function buildLodash(req, res, query) {
  var args = [lodashCli];

  // add Lo-Dash build modifier
  var modifier = query.modifier;
  if (modifier && /^[\w,]+$/.test(modifier) && modifier != 'modularize') {
    args.push(modifier);
  }

  // add options
  var opts = ['category', 'exports', 'iife', 'include', 'plus', 'minus', 'moduleId'];
  for (var length = opts.length, i = 0; i < length; i++) {
    var optName = opts[i], opt = query[optName];
    if (opt && (optName == 'iife' ? /^(?:"()"|\S+)$/.test(opt) : /^[\w,]+$/.test(opt))) {
      args.push(optName + '=' + opt);
    }
  };
  // minify?
  args.push(query.minify ? '--minify' : '--debug')
  args.push('--silent', '--stdout');
  console.log(args);

  var compiler = spawn('node', args);

  compiler.stdout.pipe(res);
  compiler.stderr.pipe(res);
  res.setHeader('content-type', 'text/plain');
}
<commit_msg>Add better error handling<commit_after>var port = Number(process.env.PORT)  || 8080;

/** Load Node.js modules */
var http = require('http'),
    path = require('path'),
    parseURL = require('url').parse,
    spawn = require('child_process').spawn;

var lodashCli = require('lodash-cli/package.json').bin.lodash;
lodashCli = require.resolve('lodash-cli/' + lodashCli);

var ecstatic = require('ecstatic');

/** Used as the static file server middleware */
var mount = ecstatic({ root: path.join(__dirname, '/public'), cache: 3600, showDir: false });

http.createServer(function(req, res) {
  var parsedURL = parseURL(req.url, true);
  if (parsedURL.pathname == '/build' && parsedURL.query) {
    buildLodash(req, res, parsedURL.query);
  } else {
    mount(req, res);
  }
}).listen(port);


/**
 * Build a Lo-Dash custom build.
 *
 * @private
 * @param {IncomingRequest} req The request entering the server.
 * @param {ServerResponse} res The server response object.
 * @param {Object} query The parsed query string object.
 */
function buildLodash(req, res, query) {
  var args = [lodashCli], errors = [];

  // add Lo-Dash build modifier
  var modifier = query.modifier;
  if (modifier && /^[a-z,]+$/i.test(modifier) && modifier != 'modularize') {
    args.push(modifier);
  } else if (modifier) {
    errors.push('Invalid modifier: ' + modifier);
  }

  // add options
  var opts = ['category', 'exports', 'iife', 'include', 'plus', 'minus', 'moduleId'];
  for (var length = opts.length, i = 0; i < length; i++) {
    var optName = opts[i], opt = query[optName];
    if (opt && (optName == 'iife' ? /^"([^"]+)"$/.test(opt) : /^[a-z,]+$/i.test(opt))) {
      args.push(optName + '=' + opt);
    } else if (opt) {
      errors.push('Invalid option `' + optName + '`: ' + opt);
    }
  };
  // minify?
  args.push(query.minify ? '--minify' : '--debug')
  args.push('--silent', '--stdout');
  console.log(args);

  res.setHeader('content-type', 'text/plain');
  if (errors.length) {
    res.end(['ERROR:'].concat(errors).join('\n'))
  } else {
    var compiler = spawn('node', args);

    compiler.stdout.pipe(res);
    compiler.stderr.pipe(res);
  }
}

{'fraction_non_alphanumeric': 0.2038782655534608, 'fraction_numerical': 0.004847831941826017, 'mean_word_length': 3.6251556662515565, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6215406', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1292, 'n_words': 294}