diff --git a/app.js b/app.js index d5a5034..68dc5c8 100644 --- a/app.js +++ b/app.js @@ -7,7 +7,9 @@ var env = process.env.NODE_ENV || 'development', fs = require('fs'), Steppy = require('twostep').Steppy, _ = require('underscore'), - reader = require('./lib/reader'), + Reader = require('./lib/reader').Reader, + BaseReaderLoader = require('./lib/reader/loader/base').Loader, + JsonReaderLoader = require('./lib/reader/loader/json').Loader, notifier = require('./lib/notifier'), ProjectsCollection = require('./lib/project').ProjectsCollection, BuildsCollection = require('./lib/build').BuildsCollection, @@ -19,6 +21,9 @@ var env = process.env.NODE_ENV || 'development', var app = new EventEmitter(), logger = libLogger('app'); +app.reader = new Reader(); +app.reader.register('json', JsonReaderLoader); + var httpServerLogger = libLogger('http server'); app.httpServer = httpServer.create(); @@ -54,7 +59,7 @@ app.httpServer.addRequestListener(function(req, res, next) { }); app.lib = {}; -app.lib.reader = reader; +app.lib.BaseReaderLoader = BaseReaderLoader; app.lib.notifier = notifier; app.lib.logger = libLogger; @@ -174,7 +179,7 @@ Steppy( }); } - reader.load(app.config.paths.data, 'config', this.slot()); + app.reader.load(app.config.paths.data, 'config', this.slot()); }, function(err, mkdirResult, config) { this.pass(mkdirResult); @@ -201,7 +206,7 @@ Steppy( function() { app.projects = new ProjectsCollection({ db: db, - reader: reader, + reader: app.reader, baseDir: app.config.paths.projects }); diff --git a/lib/reader/index.js b/lib/reader/index.js index d3b80b6..f37d3a8 100644 --- a/lib/reader/index.js +++ b/lib/reader/index.js @@ -3,20 +3,22 @@ var Steppy = require('twostep').Steppy, _ = require('underscore'), fs = require('fs'), - path = require('path'), - BaseReader = require('./base').Reader; + path = require('path'); -var constructors = { - json: require('./json').Reader + +function Reader() { + this.constructors = {}; +} + +exports.Reader = Reader; + +Reader.prototype.register = function(ext, constructor) { + this.constructors[ext] = constructor; }; -exports.BaseReader = BaseReader; +Reader.prototype.load = function(dir, name, callback) { + var self = this; -exports.register = function(ext, constructor) { - constructors[ext] = constructor; -}; - -exports.load = function(dir, name, callback) { Steppy( function() { fs.readdir(dir, this.slot()); @@ -26,19 +28,19 @@ exports.load = function(dir, name, callback) { var filePath = _(filePaths).find(function(filePath) { ext = path.extname(filePath).replace(/^\./, ''); return ( - ext in constructors && + ext in self.constructors && path.basename(filePath, '.' + ext) === name ); }); if (filePath) { - var Constructor = constructors[ext], + var Constructor = self.constructors[ext], reader = new Constructor(); reader.load(dir, name, this.slot()); } else { throw new Error( 'Can`t load "' + name + '" from "' + dir + '" using ' + - 'readers: ' + _(constructors).keys().join(', ') + 'readers: ' + _(self.constructors).keys().join(', ') ); } }, diff --git a/lib/reader/base.js b/lib/reader/loader/base.js similarity index 67% rename from lib/reader/base.js rename to lib/reader/loader/base.js index 78e13f0..c1e1574 100644 --- a/lib/reader/base.js +++ b/lib/reader/loader/base.js @@ -2,12 +2,12 @@ var Steppy = require('twostep').Steppy; -function Reader() { +function Loader() { } -exports.Reader = Reader; +exports.Loader = Loader; -Reader.prototype.load = function(dir, name, callback) { +Loader.prototype.load = function(dir, name, callback) { var self = this; Steppy( function() { @@ -23,5 +23,5 @@ Reader.prototype.load = function(dir, name, callback) { ); }; -Reader.prototype._load = function(dir, name, callback) { +Loader.prototype._load = function(dir, name, callback) { }; diff --git a/lib/reader/json.js b/lib/reader/loader/json.js similarity index 60% rename from lib/reader/json.js rename to lib/reader/loader/json.js index a6c6e4f..53d483f 100644 --- a/lib/reader/json.js +++ b/lib/reader/loader/json.js @@ -2,22 +2,22 @@ var Steppy = require('twostep').Steppy, inherits = require('util').inherits, - ParentReader = require('./base').Reader, + ParentLoader = require('./base').Loader, path = require('path'), fs = require('fs'); -function Reader() { - ParentReader.call(this); +function Loader() { + ParentLoader.call(this); } -inherits(Reader, ParentReader); +inherits(Loader, ParentLoader); -exports.Reader = Reader; +exports.Loader = Loader; -Reader.prototype.ext = 'json'; +Loader.prototype.ext = 'json'; -Reader.prototype._load = function(dir, name, callback) { +Loader.prototype._load = function(dir, name, callback) { var self = this; Steppy( function() { diff --git a/package.json b/package.json index ebd3cba..8e060cf 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "nci-projects-reloader": "0.1.1", "nci-rest-api-server": "0.1.1", "nci-static-server": "0.1.0", - "nci-yaml-reader": "0.1.0", + "nci-yaml-reader": "0.2.0", "nodemon": "1.3.7", "nrun": "0.1.4", "sinon": "1.14.1"