Sails Load Configuration without Lifting
I was making a simple grunt
task to interface with a postgres database. The project uses the [sails.js][sails] framework. I basically needed to get hold of the sails configuration inside the grunt task.
One way to accomplish this would be to lift the sails app and then access the config object:
var defaults = {
port: 1337,
log: 'silent',
environment: 'test'
};
var sails = new Sails();
sails.lift(defaults, function (err) {
if (err) {
grunt.fail.fatal('Could not lift sails', err);
return done();
}
grunt.log.writeln('DB connections' + JSON.stringify(sails.config.connections));
done(err);
});
Instead we can take the functions that just what we need.
var sailsLoader = require('sails/lib/hooks/moduleloader')({
config:{
environment: process.env.NODE_ENV || 'development',
paths:{},
appPath: process.cwd(),
dontFlattenConfig: true,
moduleloader:{
configExt:['js']
}
},
util: {
merge: require('sails/node_modules/sails-util').merge
}
});
Then, you invoke it like this:
sailsLoader.loadUserConfig(function(err, config){
//config available
});
If you want to use it inside a grunt
task definition:
grunt.registerTask('pg-drop-user', 'Drop a Postgres user', function(){
var done = this.async();
sailsLoader.loadUserConfig(function(err, config){
if (err) return done(err);
...
done();
});
});
[sails]: http://sailsjs.org