Last active
December 23, 2015 14:19
-
-
Save campbellanderson/6647794 to your computer and use it in GitHub Desktop.
Starting sails without the webserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SailsOrm = require([path to sails]'/lib/hooks/orm'); | |
var _ = require( 'lodash' ); | |
var async = require('async'); | |
function orm () | |
{ | |
/** | |
* Start the ORM layer | |
* | |
* @api private | |
*/ | |
this.init = function (sailsDir, cb) { | |
var orm = this; | |
orm.appPath = sailsDir; | |
var CaptainsLog = require(sailsDir+'/lib/logger')(orm) | |
orm.log = CaptainsLog(); | |
console.log('Loading app models...'); | |
console.log('Loading from "' + orm.appPath + '/config"' ); | |
orm.config = buildDictionary({ | |
dirname : orm.appPath + '/config', | |
filter: /(.+)\.(js)$/, | |
aggregate: true | |
}); | |
orm.config.paths = {}; | |
orm.config.paths.app = orm.appPath; | |
orm.config.paths.config = orm.appPath + '/config'; | |
orm.config.paths.models = orm.appPath + '/api/models'; | |
orm.config.paths.adapters = orm.appPath + '/api/adapters'; | |
orm.config.paths.services = orm.appPath + '/api/services'; | |
orm.defaultAdapterModule = 'sails-disk'; | |
orm.config.globals = {}; | |
orm.config.globals.models = true; | |
orm.config.globals.services = true; | |
orm.config.globals._ = true; | |
orm.config.globals.async = true; | |
orm.config.globals.sails = true; | |
orm.models = buildDictionary({ | |
dirname : orm.config.paths.models, | |
filter: /(.+)\.(js)$/, | |
aggregate: true | |
}); | |
orm.services = buildDictionary({ | |
dirname : orm.config.paths.services, | |
filter : /(.+)\.(js|coffee)$/, | |
caseSensitive: true | |
}); | |
if(orm.config.adapters.default) | |
orm.config.adapters.default = orm.config.adapters[orm.config.adapters.default]; | |
if (orm.config.globals._) global['_'] = _; | |
if (orm.config.globals.async) global['async'] = async; | |
if (orm.config.globals.sails) global['sails'] = orm; | |
if (orm.config.globals.services) { | |
_.each(orm.services,function (service,identity) { | |
var globalName = service.globalId || service.identity; | |
global[globalName] = service; | |
}); | |
} | |
console.log('Loading app adapters...'); | |
var sailsOrm = new SailsOrm(orm); | |
sailsOrm.initialize(function (err) | |
{ | |
cb && cb(err, orm); | |
}); | |
}; | |
function buildDictionary(options) { | |
var files = require('include-all')(options); | |
var dictionary = {}; | |
// Iterate through each module in the set | |
_.each(files, function(module, filename) { | |
// Build a single module, treating each source module as containers pieces of a distributed object | |
if (options.aggregate) { | |
// Check that source module is a valid object | |
if (_.isArray(module) || !_.isObject(module)) { | |
throw new Error('Invalid module:' + module); | |
} | |
else _.extend(dictionary, module); | |
} | |
// Build the module dictionary | |
else { | |
var keyName = filename; | |
// If a module is found but marked as undefined, | |
// don't actually include it in the final dictionary | |
if (typeof module === 'undefined') { | |
return; | |
} | |
// Unless the identity is explicitly disabled, or `dontLoad` option is set, | |
// If no 'identity' attribute was provided, take a guess based on the (case-insensitive) filename | |
if (!options.dontLoad && options.identity !== false) { | |
if(!module.identity) { | |
module.identity = options.replaceExpr ? filename.replace(options.replaceExpr, "") : filename; | |
module.globalId = module.identity; | |
module.identity = module.identity.toLowerCase(); | |
keyName = module.identity; | |
} | |
else { | |
module.globalId = module.identity; | |
module.identity = module.identity.toLowerCase(); | |
keyName = module.identity; | |
} | |
} | |
// Save the module's contents in our dictionary | |
// (this will actually just be `true` if the `dontLoad` option is set) | |
dictionary[keyName] = module; | |
} | |
}); | |
if(!dictionary) return {}; | |
return dictionary; | |
} | |
} | |
// Start the orm | |
orm.init(process.cwd(), function(err, orm) | |
{ | |
//kick things off | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment