Refactor code structure to be consistent with node.js conventions
indexzero opened this issue · comments
Currently the code structure for kohai resembles this:
plugins/
utils/
server.js
This goes against conventions used in other projects written by nodejitsu and the community at large. We should refactor this to the following
bin/
server
lib/
plugins/
utils/
kohai.js
Happy to take this one on, but I don't want to mess anything up since there are no tests.
I think I've done it, but I'll leave this open for comment from those more knowledgeable.
This looks good, but there are a couple of small things that you need to add on top of the restructure. The primary motivation here is that you want this library to be used programatically (i.e. from a require
statement) as well as a stand-alone application (i.e. from running node bin/server.js
).
In order to make the require work at a top level (not just by requiring individual plugins, as you do here: https://github.com/nodejitsu/kohai/blob/master/bin/server.js#L30) you need to do two things:
- Add a
main
property to yourpackage.json
file which has a value to the local path of the top level include (which I suggested adding aslib/kohai.js
) - Write
lib/kohai.js
. This is actually ridiculously simple. You simply need to make each plugin a lazy-loaded require statement. See https://github.com/nodejitsu/haibu/blob/master/lib/haibu.js#L62 for a good example of how to do this.
Once this is done the following code sample will work:
var kohai = require('kohai');
kohai.somePlugin(someOptions);
And voila! You've got a node.js module that complies with common conventions.
Forgot to link the thread in the commit message. 5a890fe
It's working this far:
var kohaitest = require('kohai')
console.log(kohaitest)
kohaitest()
That gives you a dump of your current config and starts the bot running. One could also make nconf changes like this, but there isn't much of an external api to speak of right now - all the bot's actions are tied to chat and twitter events. I think this is one of the things meant to be addressed by issue #7