<commit_before>var extend = require('node.extend');
var yaml = require('js-yaml');
var fs = require('fs');
var expressReverse = require('express-reverse');

module.exports = function(app, options) {

	options = extend(true, {
		routingFile: __dirname+'/../../app/config/routes.yml',
		controllerPath: __dirname+'/../../app/controllers',
		helperName: 'url'
	}, options || {});
	
	expressReverse(app, {helperName: options.helperName});

	initRoutes(app, options);
};

function initRoutes(app, options) {
	//Load YAML
	var routesYaml = yaml.safeLoad(fs.readFileSync(options.routingFile, 'utf8'));

	// Load all controllers
	//TODO: 
	//-search subfolders

	var controllers = {};
	fs.readdirSync(options.controllerPath).forEach(function(file) {
		var name = file.substr(0, file.indexOf('.'));
		controllers[name] = require(options.controllerPath +'/'+ file);
	});

	//Setup routes in the format 'app.method("name", "pattern", function(...)',
	//based on the YAML routes and controllers.
	for (var key in routesYaml) {
		if (routesYaml.hasOwnProperty(key)) {
			var obj = routesYaml[key];
			var split = obj.controller.split(':'),
				bundle = split[0],
				controller = split[1] + 'Controller';
		 
			obj.methods.forEach(function(method) {
				var c = controllers[bundle]();
				app[method.toLowerCase()](key, obj.pattern, c[controller] );
			});
		}
	}
}<commit_msg>InitRoutes: refactor<commit_after>var extend = require('node.extend');
var yaml = require('js-yaml');
var fs = require('fs');
var expressReverse = require('express-reverse');

module.exports = function(app, options) {

	options = extend(true, {
		routingFile: __dirname+'/../../app/config/routes.yml',
		controllerPath: __dirname+'/../../app/controllers',
		helperName: 'url'
	}, options || {});
	
	expressReverse(app, {helperName: options.helperName});

	initRoutes(app, options);
};

function initRoutes(app, options) {

	// Load all controllers
	//TODO: 
	//-search subfolders

	var controllers = {};
	fs.readdirSync(options.controllerPath).forEach(function(file) {
		var name = file.substr(0, file.indexOf('.'));
		controllers[name] = require(options.controllerPath +'/'+ file);
	});


	var parseYML = function(file) {
		
		var routesYaml = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
		for (var key in routesYaml) {
			
			var obj = routesYaml[key];

			var split = obj.controller.split(':'),
				bundle = split[0],
				controller = split[1] + 'Controller';
		 
			obj.methods.forEach(function(method) {
				var c = controllers[bundle]();
				app[method.toLowerCase()](key, obj.pattern, c[controller] );
			});
		}
	};
	
	//Load YAML
	parseYML(options.routingFile);

}
{'fraction_non_alphanumeric': 0.17803030303030304, 'fraction_numerical': 0.0030303030303030303, 'mean_word_length': 4.158203125, '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': '2778626', 'n_tokens_mistral': 959, 'n_tokens_neox': 860, 'n_words': 176}