<commit_before>////////////////////////////////////////////////////////////
// Sails
// common.js
////////////////////////////////////////////////////////////
// Define app and configuration object
var sails = {
	config: {}
};

// Utility libs
var _ = require('underscore');
_.str = require('underscore.string');
var async = require('async');

// TODO: make this disableable
//////////////////////////////
global['_'] = _;
global['sails'] = sails;
global['async'] = async;
//////////////////////////////


// Node.js dependencies
var fs = require('fs');
var path = require('path');

// Routing and HTTP server
var express = require('express');

// WebSocket server (+polyfill support)
var socketio = require('socket.io');

// Templating
var ejs = require('ejs');

// Sails ecosystem dependencies
var rigging = require('rigging');
var waterline = require('waterline');
var modules = require('sails-moduleloader');
var util = require('sails-util');

// Internal dependencies
// var util = require('./util');
var configuration = require("./configuration");
var pubsub = require('./rooms');



/**
 * Load and initialize the app
 */
exports.lift = function liftSails(userConfig, cb) {
	exports.load(userConfig, function() {
		exports.initialize(userConfig, cb);
	});
};
exports.hoist = exports.lift;
exports.start = exports.lift;




/**
 * Load the dependencies and app-specific components
 */
exports.load = function loadSails(userConfig, cb) {

	// If appPath not specified, use process.cwd() to get the app dir
	userConfig.appPath = userConfig.appPath || process.cwd();

	// Get config files (must be in /config)
	sails.config = modules.optional({
		dirname		: userConfig.appPath + '/config',
		filter		: /(.+)\.js$/,
		identity	: false
	});

	// Merge user config with defaults
	sails.config = _.extend(sails.config,configuration.build(configuration.defaults(userConfig), userConfig));

	// Initialize captains logger
	sails.log = require('./logger')(sails.config.log);

	// Pass logger down to submodules
	rigging = rigging({log: sails.log});

	// For reference
	// var someOtherModulePath = require('path').dirname(require.resolve('someOtherModule')) + '/../package.json';
	
	// Validate user config
	sails.config = configuration.validate(sails.config, userConfig);


	// EXPERIMENTAL connect v2 support
	// see https://github.com/senchalabs/connect/issues/588
	//var connect = require('connect');
	//var connectCookie = require('cookie');
	//var cookieSecret = "k3yboard_kat";
	//parseCookie = function(cookie) {
	//	return connect.utils.parseSignedCookies(connectCookie.parse(decodeURIComponent(cookie)),cookieSecret);
	//}
	var connect = require('connect');
	parseCookie = connect.utils.parseCookie;
	ConnectSession = connect.middleware.session.Session;

	// Override the environment variable so express mirrors the sails env:
	process.env['NODE_ENV'] = sails.config.environment;

	// Load app's models and custom adapters
	// Case-insensitive, using filename to determine identity
	// (default waterline adapters are included automatically)
	sails.models = modules.optional({
		dirname		: sails.config.paths.models,
		filter		: /(.+)\.js$/
	});
	sails.adapters = modules.optional({
		dirname		: sails.config.paths.adapters,
		filter		: /(.+Adapter)\.js$/,
		replaceExpr	: /Adapter/
	});

	
	// Augment models with room/socket logic (& bind context)
	for (var identity in sails.models) {
		sails.models[identity] = _.extend(sails.models[identity],pubsub);
		_.bindAll(sails.models[identity], 'subscribe', 'introduce', 'unsubscribe', 'publish', 'room');
	}

	// Start up waterline (ORM) and pass in adapters and models
	// as well as the sails logger and a copy of the default adapter configuration
	waterline({
		
		adapters: sails.adapters,
		
		collections: sails.models,
		
		log: sails.log,

		defaultAdapter: _.clone(sails.config.adapter || 'waterline-dirty')

	}, function afterWaterlineInstantiated (err, instantiatedModules){
		if (err) throw new Error(err);

		// Make instantiated adapters and collections globally accessible
		sails.adapters = instantiatedModules.adapters;
		sails.models = sails.collections = instantiatedModules.collections;

		// Load app's logic modules (case-insensitive)
		sails.controllers = modules.optional({
			dirname		: sails.config.paths.controllers,
			filter		: /(.+)Controller\.js$/, 
			replaceExpr	: /Controller/
		});
		sails.services = modules.optional({
			dirname		: sails.config.paths.services,
			filter		: /(.+)\.js$/, 
			replaceExpr	: /Service/
		});
		sails.middleware = modules.optional({
			dirname		: sails.config.paths.middleware,
			filter		: /(.+)\.js$/, 
			replaceExpr	: null
		});

		// Get federated controllers where actions are specified each in their own file
		var federatedControllers = modules.optional({
			dirname			: sails.config.paths.controllers,
			pathFilter		: /(.+)\/(.+)\.js$/
		});
		sails.controllers = _.extend(sails.controllers,federatedControllers);

		// Provide global access (if allowed in config)
		// TODO: support disablement of _, sails, and async globalization
		// if (sails.config.globals._) global['_'] = _;
		// if (sails.config.globals.async) global['async'] = async;
		// if (sails.config.globals.sails) global['sails'] = sails;
		// TODO: support models and adapter globalization (pass config into waterline)
		if (sails.config.globals.services) {
			_.each(sails.services,function (service,identity) {
				var globalName = _.str.capitalize(identity)+'Service';
				global[globalName] = service;
			});
		}
		// TODO: allow global access to even those objects to be disabled in config
		
		// Run boostrap script
		if (sails.config.bootstrap) sails.config.bootstrap(afterBootstrap);
		else afterBootstrap();

		function afterBootstrap () {
			require('./express.js').configure();
			cb && cb();
		}
	});
};
<commit_msg>Switch positioning of controller and service import to allow for use of services throughout app.<commit_after>////////////////////////////////////////////////////////////
// Sails
// common.js
////////////////////////////////////////////////////////////
// Define app and configuration object
var sails = {
	config: {}
};

// Utility libs
var _ = require('underscore');
_.str = require('underscore.string');
var async = require('async');

// TODO: make this disableable
//////////////////////////////
global['_'] = _;
global['sails'] = sails;
global['async'] = async;
//////////////////////////////


// Node.js dependencies
var fs = require('fs');
var path = require('path');

// Routing and HTTP server
var express = require('express');

// WebSocket server (+polyfill support)
var socketio = require('socket.io');

// Templating
var ejs = require('ejs');

// Sails ecosystem dependencies
var rigging = require('rigging');
var waterline = require('waterline');
var modules = require('sails-moduleloader');
var util = require('sails-util');

// Internal dependencies
// var util = require('./util');
var configuration = require("./configuration");
var pubsub = require('./rooms');



/**
 * Load and initialize the app
 */
exports.lift = function liftSails(userConfig, cb) {
	exports.load(userConfig, function() {
		exports.initialize(userConfig, cb);
	});
};
exports.hoist = exports.lift;
exports.start = exports.lift;




/**
 * Load the dependencies and app-specific components
 */
exports.load = function loadSails(userConfig, cb) {

	// If appPath not specified, use process.cwd() to get the app dir
	userConfig.appPath = userConfig.appPath || process.cwd();

	// Get config files (must be in /config)
	sails.config = modules.optional({
		dirname		: userConfig.appPath + '/config',
		filter		: /(.+)\.js$/,
		identity	: false
	});

	// Merge user config with defaults
	sails.config = _.extend(sails.config,configuration.build(configuration.defaults(userConfig), userConfig));

	// Initialize captains logger
	sails.log = require('./logger')(sails.config.log);

	// Pass logger down to submodules
	rigging = rigging({log: sails.log});

	// For reference
	// var someOtherModulePath = require('path').dirname(require.resolve('someOtherModule')) + '/../package.json';
	
	// Validate user config
	sails.config = configuration.validate(sails.config, userConfig);


	// EXPERIMENTAL connect v2 support
	// see https://github.com/senchalabs/connect/issues/588
	//var connect = require('connect');
	//var connectCookie = require('cookie');
	//var cookieSecret = "k3yboard_kat";
	//parseCookie = function(cookie) {
	//	return connect.utils.parseSignedCookies(connectCookie.parse(decodeURIComponent(cookie)),cookieSecret);
	//}
	var connect = require('connect');
	parseCookie = connect.utils.parseCookie;
	ConnectSession = connect.middleware.session.Session;

	// Override the environment variable so express mirrors the sails env:
	process.env['NODE_ENV'] = sails.config.environment;

	// Load app's models and custom adapters
	// Case-insensitive, using filename to determine identity
	// (default waterline adapters are included automatically)
	sails.models = modules.optional({
		dirname		: sails.config.paths.models,
		filter		: /(.+)\.js$/
	});
	sails.adapters = modules.optional({
		dirname		: sails.config.paths.adapters,
		filter		: /(.+Adapter)\.js$/,
		replaceExpr	: /Adapter/
	});

	
	// Augment models with room/socket logic (& bind context)
	for (var identity in sails.models) {
		sails.models[identity] = _.extend(sails.models[identity],pubsub);
		_.bindAll(sails.models[identity], 'subscribe', 'introduce', 'unsubscribe', 'publish', 'room');
	}

	// Start up waterline (ORM) and pass in adapters and models
	// as well as the sails logger and a copy of the default adapter configuration
	waterline({
		
		adapters: sails.adapters,
		
		collections: sails.models,
		
		log: sails.log,

		defaultAdapter: _.clone(sails.config.adapter || 'waterline-dirty')

	}, function afterWaterlineInstantiated (err, instantiatedModules){
		if (err) throw new Error(err);

		// Make instantiated adapters and collections globally accessible
		sails.adapters = instantiatedModules.adapters;
		sails.models = sails.collections = instantiatedModules.collections;

		// Load app's logic modules (case-insensitive)
		sails.services = modules.optional({
			dirname		: sails.config.paths.services,
			filter		: /(.+)\.js$/, 
			replaceExpr	: /Service/
		});
		sails.controllers = modules.optional({
			dirname		: sails.config.paths.controllers,
			filter		: /(.+)Controller\.js$/, 
			replaceExpr	: /Controller/
		});
		sails.middleware = modules.optional({
			dirname		: sails.config.paths.middleware,
			filter		: /(.+)\.js$/, 
			replaceExpr	: null
		});

		// Get federated controllers where actions are specified each in their own file
		var federatedControllers = modules.optional({
			dirname			: sails.config.paths.controllers,
			pathFilter		: /(.+)\/(.+)\.js$/
		});
		sails.controllers = _.extend(sails.controllers,federatedControllers);

		// Provide global access (if allowed in config)
		// TODO: support disablement of _, sails, and async globalization
		// if (sails.config.globals._) global['_'] = _;
		// if (sails.config.globals.async) global['async'] = async;
		// if (sails.config.globals.sails) global['sails'] = sails;
		if (sails.config.globals.services) {
			_.each(sails.services,function (service,identity) {
				console.log("GLOBALIZING "+identity);
				var globalName = _.str.capitalize(identity)+'Service';
				global[globalName] = service;
			});
		}
		// TODO: allow global access to even those objects to be disabled in config
		
		// Run boostrap script
		if (sails.config.bootstrap) sails.config.bootstrap(afterBootstrap);
		else afterBootstrap();

		function afterBootstrap () {
			require('./express.js').configure();
			cb && cb();
		}
	});
};

{'fraction_non_alphanumeric': 0.17156778936570355, 'fraction_numerical': 0.0008548469823901521, 'mean_word_length': 3.39812030075188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '564899', 'n_tokens_mistral': 3782, 'n_tokens_neox': 3425, 'n_words': 880}