var fs = require("fs"),
through2 = require("through2"),
Transform = require("stream").Transform,
vinyl = require("vinyl"),
async = require("async");
.
.
.
resolveDependant = function(file, stream, cb){
/**
* @var array tree - list of files[], aka driectory tree
*/
var reg = new RegExp("Catch me!", "gm"),//ignore
seriesList = []; //for threading
for(var f in tree){
seriesList.push( //ignore
function(file){ //ignore
return function(callback){ //ignore
fs.readFile(file, { encoding : "utf8" }, function(err, data){ //ignore
if(reg.exec(data)){ //trying to catch him!
stream.push( //here is time leak! For 4 files it spent 1.6s.
new vinyl({ //without previous line it spent 0.4s for same 4 files
path : file,
contents :
new Buffer(data)
})
);
}
callback();
});
}
}(tree[f])
);
}
.
.
.
this.chain = function() { //gulp.pipe(obj.chain()) call
return through2.obj(function (file, enconding, cb) {
/**
* @var array tree - list of files[], aka driectory tree
*/
if(!tree.length) //just ensure that chain was called after initialization
return cb(null, file);
var stream = this;
resolveDependant(file.path, stream, function(){
stream.push(file); //test mode ignore any input
cb(); //return control to pipe
});
});
}