change exposed reader api

This commit is contained in:
oleg 2016-03-16 22:20:08 +03:00
parent 84c6b8ee05
commit dbd84ea32f
2 changed files with 17 additions and 15 deletions

6
app.js
View File

@ -8,14 +8,13 @@ var env = process.env.NODE_ENV || 'development',
Steppy = require('twostep').Steppy,
_ = require('underscore'),
Reader = require('./lib/reader').Reader,
BaseReaderLoader = require('./lib/reader/loader/base').Loader,
JsonReaderLoader = require('./lib/reader/loader/json').Loader,
Notifier = require('./lib/notifier').Notifier,
BaseNotifierTransport = require('./lib/notifier/transport/base').Transport,
ConsoleNotifierTransport = require('./lib/notifier/transport/console').Transport,
ProjectsCollection = require('./lib/project').ProjectsCollection,
BuildsCollection = require('./lib/build').BuildsCollection,
libLogger = require('./lib/logger'),
libReader = require('./lib/reader'),
libNode = require('./lib/node'),
libCommand = require('./lib/command'),
libExecutor = require('./lib/executor'),
@ -28,7 +27,6 @@ var app = new EventEmitter(),
logger = libLogger('app');
app.reader = new Reader();
app.reader.register('json', JsonReaderLoader);
var httpServerLogger = libLogger('http server');
@ -65,9 +63,9 @@ app.httpServer.addRequestListener(function(req, res, next) {
});
app.lib = {};
app.lib.BaseReaderLoader = BaseReaderLoader;
app.lib.BaseNotifierTransport = BaseNotifierTransport;
app.lib.logger = libLogger;
app.lib.reader = libReader;
app.lib.command = libCommand;
app.lib.executor = libExecutor;
app.lib.scm = libScm;

View File

@ -3,22 +3,26 @@
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
fs = require('fs'),
path = require('path');
path = require('path'),
BaseReaderLoader = require('./loader/base').Loader,
JsonReaderLoader = require('./loader/json').Loader;
var constructors = {
json: JsonReaderLoader
};
exports.BaseReaderLoader = BaseReaderLoader;
exports.register = function(ext, constructor) {
constructors[ext] = constructor;
};
function Reader() {
this.constructors = {};
}
exports.Reader = Reader;
Reader.prototype.register = function(ext, constructor) {
this.constructors[ext] = constructor;
};
Reader.prototype.load = function(dir, name, callback) {
var self = this;
Steppy(
function() {
fs.readdir(dir, this.slot());
@ -28,19 +32,19 @@ Reader.prototype.load = function(dir, name, callback) {
var filePath = _(filePaths).find(function(filePath) {
ext = path.extname(filePath).replace(/^\./, '');
return (
ext in self.constructors &&
ext in constructors &&
path.basename(filePath, '.' + ext) === name
);
});
if (filePath) {
var Constructor = self.constructors[ext],
var Constructor = constructors[ext],
reader = new Constructor();
reader.load(dir, name, this.slot());
} else {
throw new Error(
'Can`t load "' + name + '" from "' + dir + '" using ' +
'readers: ' + _(self.constructors).keys().join(', ')
'readers: ' + _(constructors).keys().join(', ')
);
}
},