load projects config using reader

This commit is contained in:
oleg 2015-05-18 20:52:01 +03:00
parent f310239279
commit 7b2fe328a7
4 changed files with 95 additions and 17 deletions

View File

@ -3,7 +3,8 @@
var Steppy = require('twostep').Steppy,
fs = require('fs'),
path = require('path'),
_ = require('underscore');
_ = require('underscore'),
reader = require('./reader');
function Project(config) {
this.config = config;
@ -26,9 +27,6 @@ exports.load = function(baseDir, name, callback) {
fs.readdir(dir, this.slot());
},
function(err, dirContent) {
if (dirContent.indexOf('config.json') === -1) throw new Error(
'config.json is not found at project dir ' + dir
);
exports.loadConfig(dir, this.slot());
},
function(err, config) {
@ -61,21 +59,9 @@ exports.loadAll = function(baseDir, callback) {
};
exports.loadConfig = function(dir, callback) {
var configPath = path.join(dir, 'config.json');
Steppy(
function() {
fs.readFile(configPath, 'utf8', this.slot());
},
function(err, configText) {
try {
this.pass(JSON.parse(configText));
} catch(error) {
error.message = (
'Error while parsing json from config ' +
configPath + ': ' + error.message
);
throw error;
}
reader.load(dir, 'config', this.slot());
},
callback
);

27
lib/reader/base.js Normal file
View File

@ -0,0 +1,27 @@
'use strict';
var Steppy = require('twostep').Steppy;
function Reader() {
}
exports.Reader = Reader;
Reader.prototype.load = function(dir, name, callback) {
var self = this;
Steppy(
function() {
self._load(dir, name, this.slot());
},
function(err, content) {
if (err) {
err.message = 'Error while loading "' + name + '" from "' + dir +
'": \n' + err.message;
}
callback(err, content);
}
);
};
Reader.prototype._load = function(dir, name, callback) {
};

44
lib/reader/index.js Normal file
View File

@ -0,0 +1,44 @@
'use strict';
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
fs = require('fs'),
path = require('path');
var constructors = {
json: require('./json').Reader
};
exports.register = function(ext, constructor) {
constructors[ext] = constructor;
};
exports.load = function(dir, name, callback) {
Steppy(
function() {
fs.readdir(dir, this.slot());
},
function(err, filePaths) {
var ext;
var filePath = _(filePaths).find(function(filePath) {
ext = path.extname(filePath).replace(/^\./, '');
return (
ext in constructors &&
path.basename(filePath, '.' + ext) === name
);
});
if (filePath) {
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: ' + _(constructors).keys().join(', ')
);
}
},
callback
);
};

21
lib/reader/json.js Normal file
View File

@ -0,0 +1,21 @@
'use strict';
var inherits = require('util').inherits,
ParentReader = require('./base').Reader,
path = require('path');
function Reader() {
ParentReader.call(this);
}
inherits(Reader, ParentReader);
exports.Reader = Reader;
Reader.prototype.ext = 'json';
Reader.prototype._load = function(dir, name, callback) {
var content = require(path.join(dir, name + '.' + this.ext));
callback(null, content);
};