Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First draft; Tests for hostname/common settings

  • Loading branch information...
commit 0227709a8aed3e056df2ed4535920326476a6ef7 1 parent 5d5ab03
Art Skvira authored

Showing 4 changed files with 128 additions and 0 deletions. Show diff stats Hide diff stats

  1. +5 0 conf/common.js
  2. +3 0  conf/hostname.js
  3. +92 0 index.js
  4. +28 0 test.js
5 conf/common.js
... ... @@ -0,0 +1,5 @@
  1 +exports.conf = {
  2 + a: 'a',
  3 + b: 'b'
  4 +}
  5 +
3  conf/hostname.js
... ... @@ -0,0 +1,3 @@
  1 +exports.conf = {
  2 + b: 'redefined b'
  3 +}
92 index.js
... ... @@ -0,0 +1,92 @@
  1 +var sys = require('sys'),
  2 + EventEmitter = require('events').EventEmitter,
  3 + p = require('child_process'),
  4 + path = require('path');
  5 +
  6 +// You can force node-config to pick up config file different to your hostname.
  7 +exports.hostname = null;
  8 +
  9 +// By default node-config is going to look for 'conf' folder in curent directory.
  10 +// You can change that by changing the currentDirectory property
  11 +exports.currentDirectory = process.cwd();
  12 +
  13 +function _addProperty(property_name, conf) {
  14 + Object.defineProperty(
  15 + exports,
  16 + property_name,
  17 + {
  18 + get: function() {
  19 + return conf[property_name];
  20 + },
  21 + enumerable: true,
  22 + configurable: true
  23 + }
  24 + );
  25 +}
  26 +
  27 +function _processProperties(collection) {
  28 + for(property in collection) {
  29 + if(collection.hasOwnProperty(property)) {
  30 + _addProperty(property, collection);
  31 + }
  32 + }
  33 +}
  34 +
  35 +function _initInternal(hostname, callback) {
  36 + var conf = null,
  37 + conf_path = null;
  38 +
  39 + // First of all, retrieve common properties
  40 + var conf_path = path.join(exports.currentDirectory,
  41 + 'conf',
  42 + 'common.js');
  43 + try {
  44 + conf = require(conf_path).conf;
  45 + _processProperties(conf);
  46 + } catch(err) {
  47 + sys.log('Unable to locate file ' + common_path);
  48 + callback(err);
  49 + return;
  50 + }
  51 +
  52 + // Then load host-specific ones
  53 + // This is optional since there might be no host config.
  54 + try {
  55 + var conf_path = path.join(exports.currentDirectory,
  56 + 'conf',
  57 + hostname + '.js')
  58 + conf = require(conf_path).conf;
  59 + _processProperties(conf);
  60 + } catch(err) {}
  61 +
  62 + callback(null);
  63 +}
  64 +
  65 +exports.initConfig = function(callback) {
  66 +
  67 + // If hostname is set by user, do not invoke the 'hostname' command line tool.
  68 + // However still behave in async fashion.
  69 + if(exports.hostname != null) {
  70 + var eventEmitter = new EventEmitter();
  71 + eventEmitter.on('init',
  72 + function() {
  73 + _initInternal(exports.hostname, callback);
  74 + }
  75 + );
  76 + eventEmitter.emit('init');
  77 +
  78 + return;
  79 + }
  80 +
  81 + // No hostname predefined, obtain hostname andproceed to initialisation.
  82 + var hostname = p.spawn('hostname');
  83 +
  84 + hostname.stdout.on('data', function(data) {
  85 + var _hostname = String(data).replace('\n', '');
  86 + _initInternal(_hostname, callback);
  87 + });
  88 +
  89 + hostname.stderr.on('data', function(data) {
  90 + callback(data);
  91 + });
  92 +}
28 test.js
... ... @@ -0,0 +1,28 @@
  1 +require.paths.unshift('.');
  2 +
  3 +var sys = require('sys'),
  4 + conf = require('./index');
  5 +
  6 +function assert(value, message) {
  7 + if(!value) {
  8 + message = 'FAIL: ' + message;
  9 + } else {
  10 + message = 'PASS: ' + message;
  11 + }
  12 +
  13 + console.log(message);
  14 +}
  15 +
  16 +conf.hostname = 'hostname';
  17 +
  18 +conf.initConfig(
  19 + function(err) {
  20 + if(err) {
  21 + console.log(err);
  22 + return;
  23 + }
  24 +
  25 + assert(conf.a == 'a', 'conf.a == a');
  26 + assert(conf.b == 'redefined b', 'conf.b == redefined b');
  27 + }
  28 +);

0 comments on commit 0227709

Please sign in to comment.
Something went wrong with that request. Please try again.