<commit_before>'use strict';
var BBPromise = require('bluebird');
var glob = BBPromise.promisify(require('glob'));
var logger = require('@hoist/logger');
var path = require('path');
var _ = require('lodash');
var fs = BBPromise.promisifyAll(require('fs'));
var browserify = require("browserify");
var babelify = require("babelify");
var mkdirp = BBPromise.promisify(require('mkdirp'));
var streamToPromise = require('stream-to-promise');
var moment = require('moment');
var config = require('config');

module.exports = function (server) {

  return glob(path.resolve(__dirname, '../controllers/*_controller.js'))
    .then(function (files) {
      logger.info('controllers files', files);
      files.forEach(function (file) {
        logger.info('file', file);
        var Controller = require(file);
        var controller = new Controller();

        server.route(_.map(controller.routes, function (route) {
          route.config = route.config || {};
          route.config.bind = controller;
          route.config.pre = [{
            method: function (request, reply) {
              reply(controller.name);
            },
            assign: "controller"
          }];
          return route;
        }));
      });
    }).then(function () {
      server.route([{
        method: 'GET',
        path: '/css/{param*}',
        handler: {
          directory: {
            path: path.resolve(__dirname, '../assets/compiled/css'),
            lookupCompressed: true
          }
        }
      }, {
        method: 'GET',
        path: '/templates/{path*}',
        handler: function (request, reply) {
          var filePath;
          return BBPromise.try(function () {
                logger.info('requesting template');
                filePath = path.resolve(__dirname, '../assets/compiled/js/templates', request.params.path + '.js');
                var sourcePath = path.resolve(__dirname, '../views/', request.params.path + '.jsx');
                logger.info({
                  filePath: filePath,
                  sourcePath: sourcePath
                }, 'template path');

                function compileTemplate() {
                  return mkdirp(path.dirname(filePath))
                    .then(function () {
                      logger.info('creating file');
                      return streamToPromise(browserify({
                          debug: true,
                        })
                        .transform(babelify.configure({
                          optional: ["es7.objectRestSpread"]
                        }))
                        .require(sourcePath, {
                          expose: 'view'
                        })
                        .require('react', {
                          expose: 'react'
                        })
                        .require('react-transmit', {
                          expose: 'react-transmit'
                        })
                        .add(path.resolve(__dirname, '../assets/src/javascript/client.js'))
                        .bundle()
                        .pipe(fs.createWriteStream(filePath)));

                    });
                }
                if (config.get('Hoist.debug') || (!fs.existsSync(filePath) && fs.existsSync(sourcePath))) {
                  return compileTemplate();

                } else {
                  //check if we've modified the file
                  return BBPromise.all([
                    fs.statAsync(filePath),
                    fs.statAsync(sourcePath)
                  ]).spread(function (targetStats, sourceStats) {
                    var targetDate = moment(targetStats.mtime);
                    var sourceDate = moment(sourceStats.mtime);<commit_msg>:lipstick:<commit_after>'use strict';
var BBPromise = require('bluebird');
var glob = BBPromise.promisify(require('glob'));
var logger = require('@hoist/logger');
var path = require('path');
var _ = require('lodash');
var fs = BBPromise.promisifyAll(require('fs'));
var browserify = require("browserify");
var babelify = require("babelify");
var mkdirp = BBPromise.promisify(require('mkdirp'));
var streamToPromise = require('stream-to-promise');
var moment = require('moment');
var config = require('config');

module.exports = function (server) {

  return glob(path.resolve(__dirname, '../controllers/*_controller.js'))
    .then(function (files) {
      logger.info('controllers files', files);
      files.forEach(function (file) {
        logger.info('file', file);
        var Controller = require(file);
        var controller = new Controller();

        server.route(_.map(controller.routes, function (route) {
          route.config = route.config || {};
          route.config.bind = controller;
          route.config.pre = [{
            method: function (request, reply) {
              reply(controller.name);
            },
            assign: "controller"
          }];
          return route;
        }));
      });
    }).then(function () {
      server.route([{
        method: 'GET',
        path: '/css/{param*}',
        handler: {
          directory: {
            path: path.resolve(__dirname, '../assets/compiled/css'),
            lookupCompressed: true
          }
        }
      }, {
        method: 'GET',
        path: '/templates/{path*}',
        handler: function (request, reply) {
          var filePath;
          return BBPromise.try(function () {
                logger.info('requesting template');
                filePath = path.resolve(__dirname, '../assets/compiled/js/templates', request.params.path + '.js');
                var sourcePath = path.resolve(__dirname, '../views/', request.params.path + '.jsx');
                logger.info({
                  filePath: filePath,
                  sourcePath: sourcePath
                }, 'template path');

                function compileTemplate() {
                  return mkdirp(path.dirname(filePath))
                    .then(function () {
                      logger.info('creating file');
                      return streamToPromise(browserify({
                          debug: true
                        })
                        .transform(babelify.configure({
                          optional: ["es7.objectRestSpread"]
                        }))
                        .require(sourcePath, {
                          expose: 'view'
                        })
                        .require('react', {
                          expose: 'react'
                        })
                        .require('react-transmit', {
                          expose: 'react-transmit'
                        })
                        .add(path.resolve(__dirname, '../assets/src/javascript/client.js'))
                        .bundle()
                        .pipe(fs.createWriteStream(filePath)));

                    });
                }
                if (config.get('Hoist.debug') || (!fs.existsSync(filePath) && fs.existsSync(sourcePath))) {
                  return compileTemplate();

                } else {
                  //check if we've modified the file
                  return BBPromise.all([
                    fs.statAsync(filePath),
                    fs.statAsync(sourcePath)
                  ]).spread(function (targetStats, sourceStats) {
                    var targetDate = moment(targetStats.mtime);
                    var sourceDate = moment(sourceStats.mtime);
{'fraction_non_alphanumeric': 0.13045234248788368, 'fraction_numerical': 0.00026925148088314486, 'mean_word_length': 1.4854466376714621, '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': '7030993', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1773, 'n_words': 347}