Skip to content

Commit

Permalink
separate readDir, avoid fs.stat() in two places
Browse files Browse the repository at this point in the history
  • Loading branch information
robrich committed Nov 4, 2013
1 parent 135ca15 commit fa594e9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
14 changes: 4 additions & 10 deletions lib/bufferFile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
var fs = require('fs');

module.exports = function (file, cb) {
fs.stat(file.path, function (err, stat) {
if (stat.isDirectory()) {
file.isDirectory = true;
return cb(null, file);
}
fs.readFile(file.path, function (err, data) {
if (err) return cb(err);
file.contents = data;
cb(null, file);
});
fs.readFile(file.path, function (err, data) {
if (err) return cb(err);
file.contents = data;
cb(null, file);
});
};
20 changes: 2 additions & 18 deletions lib/createInputStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ var es = require('event-stream');
var gs = require('glob-stream');
var formatFile = require('./formatFile');

var bufferFile = require('./bufferFile');
var streamFile = require('./streamFile');
var getContents = require('./getContents');

module.exports = function(glob, opt) {
if (!opt) opt = {};
Expand All @@ -13,20 +12,5 @@ module.exports = function(glob, opt) {
var globStream = gs.create(glob, opt.glob);
var stream = globStream.pipe(es.map(formatFile));

// dont read anything - just pass names
if (!opt.read) {
return stream;
}

// read and pass full contents
if (opt.buffer) {
return stream.pipe(es.map(bufferFile));
}

// dont buffer anything - just pass streams
if (!opt.buffer) {
return stream.pipe(es.map(streamFile));
}

return stream;
return stream.pipe(getContents(opt));
};
32 changes: 32 additions & 0 deletions lib/getContents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var es = require('event-stream');
var fs = require('fs');

var readDir = require('./readDir');
var bufferFile = require('./bufferFile');
var streamFile = require('./streamFile');

module.exports = function(opt) {
return es.map(function (file, cb) {

// don't read anything - just pass names
if (!opt.read) {
return cb(null, file);
}

fs.stat(file.path, function (err, stat) {

// don't fail to read a directory
if (stat.isDirectory()) {
return readDir(file, cb);
}

// read and pass full contents
if (opt.buffer) {
return bufferFile(file, cb);
}

// dont buffer anything - just pass streams
return streamFile(file, cb);
});
});
};
4 changes: 4 additions & 0 deletions lib/readDir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function (file, cb) {
file.isDirectory = true;
cb(null, file);
};
10 changes: 2 additions & 8 deletions lib/streamFile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
var fs = require('fs');

module.exports = function (file, cb) {
fs.stat(file.path, function (err, stat) {
if (stat.isDirectory()) {
file.isDirectory = true;
return cb(null, file);
}
file.contents = fs.createReadStream(file.path);
cb(null, file);
});
file.contents = fs.createReadStream(file.path);
cb(null, file);
};

0 comments on commit fa594e9

Please sign in to comment.