<commit_before>var path = require('path');
var fs = require('fs');
var express = require('express');
var gulp = require('gulp');
var webpack = require('webpack');
var Router = require('react-router');

var webpackConfig = require('./webpack.config.js');
var indexStaticWatcher = require('./lib/index-static-watcher').create();
var gulpfile = require('./gulpfile');
var PORT = process.env.PORT || 8008;
var PRODUCTION = (process.env.NODE_ENV === 'production');
var DIST_DIR = path.join(__dirname, 'dist');
var WATCH_DELAY = 300;

var indexStatic;
var router;
var app = express();
var webpackCompiler = webpack(webpackConfig);

var startApp = function() {
  app.listen(PORT, function() {
    console.log('Listening on port', PORT);
  });
};

var updateIndexStatic = function(newIndexStatic) {
  indexStatic = newIndexStatic;
  router = Router.create({
    routes: indexStatic.routes
  });
};

if (!fs.existsSync(DIST_DIR)) {
  fs.mkdirSync(DIST_DIR);
}

app.use(function(req, res, next) {
  if (!PRODUCTION && !router) {
    return res.send('Please wait while the server-side bundle regenerates.');
  }
  if (!router.match(req.url)) {
    return next('route');
  }
  indexStatic.generate(req.url, {}, function(err, html) {
    if (err) {
      return next(err);
    }
    return res.type('html').send(html);
  });
});

app.use(express.static(DIST_DIR));

if (!module.parent) {
  console.log('Initializing server.');

  if (PRODUCTION) {
    console.log([
      'Production mode enabled. Note that "npm install" is assumed to',
      'have recently been run with NODE_ENV="production". If this is not',
      'the case, some or all static assets may be out of date.'
    ].join('\n'));
    indexStaticWatcher.build(function(err, newIndexStatic) {
      if (err) {
        throw err;
      }

      console.log('Built server-side bundle.');
      updateIndexStatic(newIndexStatic);
      startApp();
    });
  } else {
    require('./lib/developer-help')();

    indexStaticWatcher.watch(WATCH_DELAY, function(newIndexStatic) {
      updateIndexStatic(newIndexStatic);
      console.log('Rebuilt server-side bundle.');
    });

    gulp.start('watch-webpack');
    gulp.start('less');
    gulp.start('copy-static-files');
    gulp.start('watch-static-files');

    gulp.watch(gulpfile.LESS_FILES, ['less']).on('change', function() {
      console.log('Rebuilding LESS files.');
    });

    startApp();
  }
}<commit_msg>refactor app.js a bit.<commit_after>var path = require('path');
var fs = require('fs');
var express = require('express');
var gulp = require('gulp');
var webpack = require('webpack');
var Router = require('react-router');

var webpackConfig = require('./webpack.config.js');
var indexStaticWatcher = require('./lib/index-static-watcher').create();
var gulpfile = require('./gulpfile');
var PORT = process.env.PORT || 8008;
var PRODUCTION = (process.env.NODE_ENV === 'production');
var DIST_DIR = path.join(__dirname, 'dist');
var WATCH_DELAY = 300;

var indexStatic;
var router;
var app = express();
var webpackCompiler = webpack(webpackConfig);

var startApp = function() {
  app.listen(PORT, function() {
    console.log('Listening on port', PORT);
  });
};

var startDevApp = function() {
  require('./lib/developer-help')();

  indexStaticWatcher.watch(WATCH_DELAY, function(newIndexStatic) {
    updateIndexStatic(newIndexStatic);
    console.log('Rebuilt server-side bundle.');
  });

  gulp.start('watch-webpack');
  gulp.start('less');
  gulp.start('copy-static-files');
  gulp.start('watch-static-files');

  gulp.watch(gulpfile.LESS_FILES, ['less']).on('change', function() {
    console.log('Rebuilding LESS files.');
  });

  startApp();
};

var startProdApp = function() {
  console.log([
    'Production mode enabled. Note that "npm install" is assumed to',
    'have recently been run with NODE_ENV="production". If this is not',
    'the case, some or all static assets may be out of date.'
  ].join('\n'));
  indexStaticWatcher.build(function(err, newIndexStatic) {
    if (err) {
      throw err;
    }

    console.log('Built server-side bundle.');
    updateIndexStatic(newIndexStatic);
    startApp();
  });
};

var updateIndexStatic = function(newIndexStatic) {
  indexStatic = newIndexStatic;
  router = Router.create({
    routes: indexStatic.routes
  });
};

if (!fs.existsSync(DIST_DIR)) {
  fs.mkdirSync(DIST_DIR);
}

app.use(function(req, res, next) {
  if (!PRODUCTION && !router) {
    return res.send('Please wait while the server-side bundle regenerates.');
  }
  if (!router.match(req.url)) {
    return next('route');
  }
  indexStatic.generate(req.url, {}, function(err, html) {
    if (err) {
      return next(err);
    }
    return res.type('html').send(html);
  });
});

app.use(express.static(DIST_DIR));

if (!module.parent) {
  console.log('Initializing server.');

  PRODUCTION ? startProdApp() : startDevApp();
}
{'fraction_non_alphanumeric': 0.16851395730706076, 'fraction_numerical': 0.0028735632183908046, 'mean_word_length': 3.992827868852459, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1800404', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1561, 'n_words': 351}