mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-01-25 19:46:16 +00:00
50 lines
1.0 KiB
JavaScript
50 lines
1.0 KiB
JavaScript
'use strict';
|
|
|
|
var Steppy = require('twostep').Steppy,
|
|
_ = require('underscore'),
|
|
fs = require('fs'),
|
|
path = require('path');
|
|
|
|
|
|
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());
|
|
},
|
|
function(err, filePaths) {
|
|
var ext;
|
|
var filePath = _(filePaths).find(function(filePath) {
|
|
ext = path.extname(filePath).replace(/^\./, '');
|
|
return (
|
|
ext in self.constructors &&
|
|
path.basename(filePath, '.' + ext) === name
|
|
);
|
|
});
|
|
|
|
if (filePath) {
|
|
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: ' + _(self.constructors).keys().join(', ')
|
|
);
|
|
}
|
|
},
|
|
callback
|
|
);
|
|
};
|