load projects config using reader
This commit is contained in:
parent
f310239279
commit
7b2fe328a7
@ -3,7 +3,8 @@
|
|||||||
var Steppy = require('twostep').Steppy,
|
var Steppy = require('twostep').Steppy,
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
_ = require('underscore');
|
_ = require('underscore'),
|
||||||
|
reader = require('./reader');
|
||||||
|
|
||||||
function Project(config) {
|
function Project(config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
@ -26,9 +27,6 @@ exports.load = function(baseDir, name, callback) {
|
|||||||
fs.readdir(dir, this.slot());
|
fs.readdir(dir, this.slot());
|
||||||
},
|
},
|
||||||
function(err, dirContent) {
|
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());
|
exports.loadConfig(dir, this.slot());
|
||||||
},
|
},
|
||||||
function(err, config) {
|
function(err, config) {
|
||||||
@ -61,21 +59,9 @@ exports.loadAll = function(baseDir, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.loadConfig = function(dir, callback) {
|
exports.loadConfig = function(dir, callback) {
|
||||||
var configPath = path.join(dir, 'config.json');
|
|
||||||
Steppy(
|
Steppy(
|
||||||
function() {
|
function() {
|
||||||
fs.readFile(configPath, 'utf8', this.slot());
|
reader.load(dir, 'config', 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;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
callback
|
callback
|
||||||
);
|
);
|
||||||
|
27
lib/reader/base.js
Normal file
27
lib/reader/base.js
Normal 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
44
lib/reader/index.js
Normal 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
21
lib/reader/json.js
Normal 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);
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user