How to setup a custom config loader?

34 views
Skip to first unread message

Jeremy

unread,
May 31, 2012, 8:12:06 AM5/31/12
to Hook.io
Yesterday I noticed that the latest version of Hook (0.8.10) is
working on Windows again!

I had hacked the previous version to use my own loader but would
rather override this default config loader this time than re-writing
part of the hook code. To do this I've created a base type (Service)
that derives from Hook, in it I would like to simply override the
config loader to load our configurations. My simple quick attempt at
just giving it a new config function didn't work, so I then tried
setting noconfig to true and leaving my custom config function. This
resulted in getting back an error about no 'log' method being
available.

TypeError: Object [object Object] has no method 'log'

So, I'm curious, what is the proper way to setup a custom config
loader in 8.10?

Thanks,
- Jeremy

Jeremy Darling

unread,
May 31, 2012, 8:22:50 AM5/31/12
to Hook.io
Sorry, wrong error. What I get back is an error talking about "TypeError: Cannot read property '0' of undefined"

The complete error is:
C:\node temp\hooktest\node_modules\hook.io\lib\hookio\hook.js:288
  if(this._[0] === 'init') {
           ^
TypeError: Cannot read property '0' of undefined
    at [object Object].<anonymous> (C:\node temp\hooktest\node_modules\hook.io\lib\hookio\hook.js:288:12)
    at [object Object].<anonymous> (C:\node temp\hooktest\lib\serviceBaseTypes\ServiceCommon.js:9:8)
    at new <anonymous> (C:\node temp\hooktest\lib\serviceBaseTypes\Service.js:7:17)
    at Object.<anonymous> (C:\node temp\hooktest\bin\beaker.js:3:14)
    at Module._compile (module.js:446:26)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Array.0 (module.js:484:10)
    at EventEmitter._tickCallback (node.js:190:38)

So it seems I need to setup the _ property now by hand... Any ideas?

 - Jeremy

Jeremy Darling

unread,
May 31, 2012, 9:21:58 AM5/31/12
to Hook.io
Never-mind figured it out.  Because Hook uses optimist and optimist automatically sets up an _ array if you just set this._ = [] everything works fine.  Or setup your config loader to also use optimist for command line options and your golden.

Just for the record in case anyone else runs into this:

var Service = exports.Service = function(options){
  var self = this;
  options=options||{};
  if(typeof(options)==='string') options = {name: options};
  Hook.call(self, options);
};

util.inherits(Service, Hook);

Service.prototype.config = function(options){
  var self = this;
  var config = {};
  configLoader().applyto(self, {section: options.name, defaults: options});
  console.log(configLoader.argv);
}


Then inside your config loader either:
  this._ = []

Or using optimist (copied directly then slightly modified from Hook/config.js):

    function checkProperty (key, value) {
      if(typeof value === "function") {
        throw new Error('You are attempting to override prototype method: "' + key + '". This is probably a bad idea.');
      }
    }
    var argv = optimist.options({}).argv;
    Object.keys(argv).forEach(function (o){
      var reserved = ["hook-port", "hook-host"];
      if (reserved.indexOf(o) === -1) {
        checkProperty(o, target[o]);
        target[o] = argv[o];
      }
    });
Reply all
Reply to author
Forward
0 new messages