Browse files

First draft; Tests for hostname/common settings

  • Loading branch information...
1 parent 5d5ab03 commit 0227709a8aed3e056df2ed4535920326476a6ef7 Artem Skvira committed Oct 18, 2010
Showing with 128 additions and 0 deletions.
  1. +5 −0 conf/common.js
  2. +3 −0 conf/hostname.js
  3. +92 −0 index.js
  4. +28 −0 test.js
View
5 conf/common.js
@@ -0,0 +1,5 @@
+exports.conf = {
+ a: 'a',
+ b: 'b'
+}
+
View
3 conf/hostname.js
@@ -0,0 +1,3 @@
+exports.conf = {
+ b: 'redefined b'
+}
View
92 index.js
@@ -0,0 +1,92 @@
+var sys = require('sys'),
+ EventEmitter = require('events').EventEmitter,
+ p = require('child_process'),
+ path = require('path');
+
+// You can force node-config to pick up config file different to your hostname.
+exports.hostname = null;
+
+// By default node-config is going to look for 'conf' folder in curent directory.
+// You can change that by changing the currentDirectory property
+exports.currentDirectory = process.cwd();
+
+function _addProperty(property_name, conf) {
+ Object.defineProperty(
+ exports,
+ property_name,
+ {
+ get: function() {
+ return conf[property_name];
+ },
+ enumerable: true,
+ configurable: true
+ }
+ );
+}
+
+function _processProperties(collection) {
+ for(property in collection) {
+ if(collection.hasOwnProperty(property)) {
+ _addProperty(property, collection);
+ }
+ }
+}
+
+function _initInternal(hostname, callback) {
+ var conf = null,
+ conf_path = null;
+
+ // First of all, retrieve common properties
+ var conf_path = path.join(exports.currentDirectory,
+ 'conf',
+ 'common.js');
+ try {
+ conf = require(conf_path).conf;
+ _processProperties(conf);
+ } catch(err) {
+ sys.log('Unable to locate file ' + common_path);
+ callback(err);
+ return;
+ }
+
+ // Then load host-specific ones
+ // This is optional since there might be no host config.
+ try {
+ var conf_path = path.join(exports.currentDirectory,
+ 'conf',
+ hostname + '.js')
+ conf = require(conf_path).conf;
+ _processProperties(conf);
+ } catch(err) {}
+
+ callback(null);
+}
+
+exports.initConfig = function(callback) {
+
+ // If hostname is set by user, do not invoke the 'hostname' command line tool.
+ // However still behave in async fashion.
+ if(exports.hostname != null) {
+ var eventEmitter = new EventEmitter();
+ eventEmitter.on('init',
+ function() {
+ _initInternal(exports.hostname, callback);
+ }
+ );
+ eventEmitter.emit('init');
+
+ return;
+ }
+
+ // No hostname predefined, obtain hostname andproceed to initialisation.
+ var hostname = p.spawn('hostname');
+
+ hostname.stdout.on('data', function(data) {
+ var _hostname = String(data).replace('\n', '');
+ _initInternal(_hostname, callback);
+ });
+
+ hostname.stderr.on('data', function(data) {
+ callback(data);
+ });
+}
View
28 test.js
@@ -0,0 +1,28 @@
+require.paths.unshift('.');
+
+var sys = require('sys'),
+ conf = require('./index');
+
+function assert(value, message) {
+ if(!value) {
+ message = 'FAIL: ' + message;
+ } else {
+ message = 'PASS: ' + message;
+ }
+
+ console.log(message);
+}
+
+conf.hostname = 'hostname';
+
+conf.initConfig(
+ function(err) {
+ if(err) {
+ console.log(err);
+ return;
+ }
+
+ assert(conf.a == 'a', 'conf.a == a');
+ assert(conf.b == 'redefined b', 'conf.b == redefined b');
+ }
+);

0 comments on commit 0227709

Please sign in to comment.