Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/plugins'
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed May 23, 2014
2 parents 724c692 + 72647bb commit 75d87d9
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 22 deletions.
22 changes: 21 additions & 1 deletion lib/server/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function applyConfig(command, config){
if (command.hasOption(name))
command.setOption(name, config[name]);

['ignore', 'rewrite', 'handler'].forEach(function(name){
['ignore', 'rewrite', 'handler', 'preprocess'].forEach(function(name){
if (config[name])
command.values[name] = config[name];
});
Expand Down Expand Up @@ -49,6 +49,26 @@ function normOptions(options){
});
}

var preprocess = options.preprocess;
options.preprocess = {};

for (var key in preprocess)
{
var handlerList = preprocess[key];

if (!Array.isArray(handlerList))
{
if (typeof handlerList == 'string')
handlerList = [handlerList];
else
handlerList = [];
}

options.preprocess[key] = handlerList.map(function(fn){
return path.normalize(path.resolve(options._configPath || '.', fn));
});
}

return options;
}

Expand Down
28 changes: 28 additions & 0 deletions lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ function launch(config){
}
}

if (Object.keys(options.preprocess))
{
console.log('Preprocessors:')
for (var key in options.preprocess)
{
options.preprocess[key].forEach(function(preprocessorPath){
try {
var preprocessor = require(preprocessorPath);

if (typeof preprocessor == 'function')
{
files.addPreprocessor(key, preprocessor);
console.log(' ' + chalk.green(key) + ' ' + preprocessorPath);
}
else
{
logWarn('preprocess', 'Preprocessor `' + preprocessorPath + '` is not a function.');
process.exit();
}
} catch(e) {
logWarn('preprocess', 'Error on preprocessor `' + preprocessorPath + '` load: ' + e);
process.exit();
}
});
}
console.log();
}


//
// files
Expand Down
75 changes: 54 additions & 21 deletions lib/server/modules/files.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
var mime = require('mime');
var fs = require('fs');
var path = require('path');

var READ_LIMIT = 64;
var readingCount = 0;
var filesToRead = [];
var processingPaths = {};
var readCallbacks = [];
var preprocess = {};

function checkQueue(){
if (readingCount < READ_LIMIT && filesToRead.length)
readFile(filesToRead.shift());
}

function readFile(fn){
var contentType = mime.lookup(fn, 'text/plain');
var isTextFile = /^text\/|^application\/(javascript|json|.+\+xml$)|\+xml$/.test(contentType);

if (require('path').extname(fn) == '.dot')
isTextFile = true;

readingCount++;
fs.readFile(fn, isTextFile ? 'utf8' : null, function(err, content){
var callbacks = processingPaths[fn];

delete processingPaths[fn];
readingCount--;

for (var i = 0, callback; callback = callbacks[i]; i++)
callback(err, content, fn);

checkQueue();
});
}

function addFileToQueue(fn, callback){
if (processingPaths[fn]) {
if (typeof callback == 'function')
Expand All @@ -51,6 +32,33 @@ function addFileToQueue(fn, callback){
filesToRead.push(fn);
}

function readFile(fn){
var contentType = mime.lookup(fn, 'text/plain');
var isTextFile = /^text\/|^application\/(javascript|json|.+\+xml$)|\+xml$/.test(contentType);

readingCount++;

fs.readFile(fn, isTextFile ? 'utf8' : null, function(err, content){
readingCount--;
checkQueue();

var preprocessors = preprocess[path.extname(fn)];
if (!err && preprocessors)
preprocessors[0](fn, content);
else
runCallbacks(err, fn, content);
});
}

function runCallbacks(err, filename, content){
var callbacks = processingPaths[filename];

for (var i = 0, callback; callback = callbacks[i]; i++)
callback(err, content, filename);

delete processingPaths[filename];
}

module.exports = {
readFile: addFileToQueue,
addReadCallback: function(fn){
Expand All @@ -60,5 +68,30 @@ module.exports = {
processingPaths[fn].splice(pos, 0, fn);

readCallbacks.push(fn);
},
addPreprocessor: function(type, fn){
if (!preprocess[type])
preprocess[type] = [];

var preprocessors = preprocess[type];
var idx = preprocessors.length;

preprocessors.push(function(filename, content){
try {
fn(content, function(err, res){
var next = preprocessors[idx + 1];

if (err)
return runCallbacks(err, filename);

if (typeof next == 'function')
next(filename, res);
else
runCallbacks(null, filename, res);
});
} catch (e) {
runCallbacks(e, filename);
}
});
}
};

0 comments on commit 75d87d9

Please sign in to comment.