forked from cliftonc/calipso
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
480 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* Configuration library | ||
*/ | ||
|
||
var rootpath = process.cwd() + '/', | ||
path = require('path'), | ||
step = require('step'), | ||
_ = require('underscore'), | ||
fs = require('fs'); | ||
|
||
/** | ||
* Config object, wrapper for nconf | ||
* @type | ||
* @options | ||
*/ | ||
|
||
function Configuration(options) { | ||
|
||
// Defaults | ||
this.type = options && options.type ? options.type : 'file'; | ||
this.env = options && options.env ? options.env : (process.env.NODE_ENV || 'development'); | ||
this.path = options && options.path ? options.path : path.join(rootpath, 'conf'); | ||
this.defaultConfig = options && options.defaultConfig ? options.defaultConfig : path.join(rootpath, 'lib', 'conf', 'default.json'); | ||
this.file = path.join(this.path, this.env + '.json'); | ||
|
||
// Track if changes have been made to config | ||
this.dirty = false; | ||
|
||
} | ||
|
||
Configuration.prototype.init = function(next) { | ||
if (typeof next !== 'function') next = function(err) { | ||
if (err) console.error(err.message) | ||
}; | ||
this.nconf = new require('nconf'); | ||
this.load(next); | ||
} | ||
|
||
/** | ||
* Check to see if configuration for this environment | ||
* doesn't exist, if so, it loads the default from default.json. | ||
*/ | ||
Configuration.prototype.check = function() { | ||
|
||
if (!path.existsSync(this.file)) { | ||
try { | ||
var defaultFile = fs.readFileSync(this.defaultConfig); | ||
// Parse it to make sure there are no errors | ||
defaultFile = JSON.stringify(JSON.parse(defaultFile), true); | ||
fs.writeFileSync(this.file, defaultFile); | ||
} catch (ex) { | ||
return ex.message; | ||
} | ||
return; | ||
} else { | ||
return; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Load the configuration | ||
*/ | ||
Configuration.prototype.load = function(next) { | ||
|
||
// Check if config exists for this environment or default it | ||
var checkConfig = this.check(); | ||
if (!checkConfig) { | ||
|
||
// Initialise nconf | ||
try { | ||
this.nconf.use(this.type, this); | ||
} catch (ex) { | ||
return next(ex); | ||
} | ||
|
||
this.nconf.load(next); | ||
|
||
} else { | ||
|
||
next(new Error("Unable to load configuration defined in " + this.env + ".json, there may be a problem with the default configuration in " + this.defaultConfig + ", reason: " + checkConfig)); | ||
|
||
} | ||
|
||
} | ||
|
||
/** | ||
* Get config - wrapper | ||
*/ | ||
Configuration.prototype.get = function(key) { | ||
return this.nconf.get(key); | ||
} | ||
|
||
/** | ||
* Get config for module - wrapper | ||
*/ | ||
Configuration.prototype.getModuleConfig = function(moduleName, key) { | ||
var moduleKey = 'modules:' + moduleName + ':config' + (key ? ':' + key : ''); | ||
return this.nconf.get(moduleKey); | ||
} | ||
|
||
|
||
/** | ||
* Set config | ||
*/ | ||
Configuration.prototype.set = function(key, value) { | ||
this.dirty = true; | ||
this.nconf.set(key, value); | ||
} | ||
|
||
/** | ||
* Set config for module - wrapper | ||
*/ | ||
Configuration.prototype.setModuleConfig = function(moduleName, key, value) { | ||
var moduleKey = 'modules:' + moduleName + ':config' + (key ? ':' + key : ''); | ||
this.dirty = true; | ||
this.nconf.set(moduleKey, value); | ||
} | ||
|
||
/** | ||
* Set default config for module - wrapper | ||
*/ | ||
Configuration.prototype.setDefaultModuleConfig = function(moduleName, config) { | ||
|
||
var moduleKey = 'modules:' + moduleName + ':config'; | ||
this.dirty = true; | ||
|
||
// Extract the defaults from the config | ||
var defaultConfig = _.reduce(_.keys(config), function(memo, key) { | ||
memo[key] = config[key]. | ||
default; | ||
return memo; | ||
}, {}) | ||
|
||
this.nconf.set(moduleKey, defaultConfig); | ||
|
||
} | ||
|
||
/** | ||
* Save config | ||
*/ | ||
Configuration.prototype.save = function(next) { | ||
this.dirty = false; | ||
this.nconf.save(next); | ||
} | ||
|
||
/** | ||
* Set & save config | ||
*/ | ||
|
||
Configuration.prototype.setSave = function(key, value, next) { | ||
this.set(key, value); | ||
this.dirty = false; | ||
this.save(next); | ||
} | ||
|
||
/** | ||
* Export the config object | ||
*/ | ||
module.exports = Configuration; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Mocha test case for core configuration library | ||
*/ | ||
var should = require('should'), | ||
fs = require('fs'), | ||
rootpath = process.cwd() + '/', | ||
path = require('path'), | ||
calipsoHelper = require('./helpers/calipsoHelper'), | ||
calipso = calipsoHelper.calipso, | ||
Client = require('./helpers/require')('client/Client'); | ||
|
||
describe('Client', function(){ | ||
|
||
before(function(){ | ||
// | ||
}); | ||
|
||
describe('Adding scripts', function(){ | ||
|
||
it('I can add a basic client script', function(done){ | ||
var client = new Client(), script = {name:'test',url:'/test.js',weight:10}; | ||
client.addScript(script); | ||
client.scripts.length.should.equal(1); | ||
client.scripts[0].should.equal(script); | ||
done(); | ||
}); | ||
|
||
it('If there is no name, it assigns the url as the name', function(done){ | ||
var client = new Client(), script = {url:'/test.js', weight:10}, scriptWithName = {url:'/test.js',weight:10, name:'/test.js'}; | ||
client.addScript(script); | ||
client.scripts.length.should.equal(1); | ||
client.scripts[0].should.eql(scriptWithName); | ||
done(); | ||
}); | ||
|
||
it('I can add core scripts just by name', function(done){ | ||
var client = new Client(), script = 'calipso', scriptWithName = {key:'calipso', url:'calipso.js',weight:-50, name:'calipso.js'}; | ||
client.addScript(script); | ||
client.scripts.length.should.equal(1); | ||
client.scripts[0].should.eql(scriptWithName); | ||
done(); | ||
}); | ||
|
||
it('I can add a script just by name', function(done){ | ||
var client = new Client(), script = '/myscript.js', scriptWithName = {url:'/myscript.js',weight:0, name:'/myscript.js'}; | ||
client.addScript(script); | ||
client.scripts.length.should.equal(1); | ||
client.scripts[0].should.eql(scriptWithName); | ||
done(); | ||
}); | ||
|
||
|
||
it('Adding a script twice does not duplicate it', function(done){ | ||
var client = new Client(), script = {name:'test',url:'/test.js',weight:10}; | ||
|
||
client.addScript(script); | ||
client.addScript(script); | ||
|
||
client.scripts.length.should.equal(1); | ||
client.scripts[0].should.equal(script); | ||
done(); | ||
|
||
}); | ||
|
||
|
||
}); | ||
|
||
describe('Adding styles', function(){ | ||
|
||
it('I can add a basic client style', function(done){ | ||
var client = new Client(), style = {name:'test',url:'/test.css',weight:10}; | ||
client.addStyle(style); | ||
client.styles.length.should.equal(1); | ||
client.styles[0].should.equal(style); | ||
done(); | ||
}); | ||
|
||
it('If there is no name, it assigns the url as the name', function(done){ | ||
var client = new Client(), style = {url:'/test.css', weight:10}, styleWithName = {url:'/test.css',weight:10, name:'/test.css'}; | ||
client.addStyle(style); | ||
client.styles.length.should.equal(1); | ||
client.styles[0].should.eql(styleWithName); | ||
done(); | ||
}); | ||
|
||
it('I can add a style by just passing in a url', function(done){ | ||
var client = new Client(), style = '/test.css', styleWithName = {url:'/test.css',weight:0, name:'/test.css'}; | ||
client.addStyle(style); | ||
client.styles.length.should.equal(1); | ||
client.styles[0].should.eql(styleWithName); | ||
done(); | ||
}); | ||
|
||
|
||
}); | ||
|
||
describe('Listing scripts and styles', function(){ | ||
|
||
it('Listing scripts satisfies the ordering defined', function(done){ | ||
var client = new Client(), | ||
s1 = {url:'/test1.js', weight:10}, | ||
s2 = {url:'/test2.js', weight:-10}; | ||
|
||
client.addScript(s1); | ||
client.addScript(s2); | ||
|
||
client.listScripts(function(err, scripts) { | ||
should.not.exist(err); | ||
scripts.should.include('<script title="/test2.js" src="/test2.js"></script>\r\n<script title="/test1.js" src="/test1.js">'); | ||
done(); | ||
}) | ||
|
||
}); | ||
|
||
it('Listing styles satisfies the ordering defined', function(done){ | ||
var client = new Client(), | ||
s1 = {url:'/test1.css', weight:10}, | ||
s2 = {url:'/test2.css', weight:-10}; | ||
|
||
client.addStyle(s1); | ||
client.addStyle(s2); | ||
|
||
client.listStyles(function(err, styles) { | ||
should.not.exist(err); | ||
styles.should.include('<link rel="stylesheet" title="/test2.css" href="/test2.css"/>\r\n<link rel="stylesheet" title="/test1.css" href="/test1.css"/>'); | ||
done(); | ||
}) | ||
|
||
}); | ||
|
||
}); | ||
|
||
after(function() { | ||
|
||
}) | ||
|
||
}); | ||
|
Oops, something went wrong.