Skip to content

Commit

Permalink
Merge pull request #20 from rlanore/master
Browse files Browse the repository at this point in the history
Add doneOnErr flags to options and Update readme
  • Loading branch information
fshost committed Aug 21, 2015
2 parents 2552b28 + 9bf6ecf commit 2ab0c06
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 13 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var dir = require('node-dir');
```

#### readFiles( dir, [options], fileCallback, [finishedCallback] )
#### readFilesStream( dir, [options], streamCallback, [finishedCallback] )
Sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. The options and finishedCallback arguments are not required.

Valid options are:
Expand All @@ -27,6 +28,7 @@ Valid options are:
- reverse: sort files in each directory in descending order
- shortName: whether to aggregate only the base filename rather than the full filepath
- sort: sort files in each directory in ascending order (defaults to true)
- doneOnErr: control if done function called on error (defaults to true)

A reverse sort can also be achieved by setting the sort option to 'reverse', 'desc', or 'descending' string value.

Expand All @@ -45,6 +47,24 @@ dir.readFiles(__dirname,
console.log('finished reading files:', files);
});

// display contents of huge files in this script's directory
dir.readFilesStream(__dirname,
function(err, stream, next) {
if (err) throw err;
var content = '';
stream.on('data',function(buffer) {
content += buffer.toString();
});
stream.on('end',function() {
console.log('content:', content);
next();
});
},
function(err, files){
if (err) throw err;
console.log('finished reading files:', files);
});

// match only filenames with a .txt extension and that don't start with a `.´
dir.readFiles(__dirname, {
match: /.txt$/,
Expand Down
17 changes: 11 additions & 6 deletions lib/readfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ function readFiles(dir, options, callback, complete) {
};
options = extend({
recursive: true,
encoding: 'utf8'
encoding: 'utf8',
doneOnErr: true
}, options);
var files = [];

Expand All @@ -62,8 +63,10 @@ function readFiles(dir, options, callback, complete) {

fs.readdir(dir, function(err, list) {
if (err)  {
if (err.code === 'EACCES') return done();
return done(err);
if (options.doneOnErr === true) {
if (err.code === 'EACCES') return done();
return done(err);
}
}
var i = 0;

Expand All @@ -78,13 +81,13 @@ function readFiles(dir, options, callback, complete) {
if (!filename) return done(null, files);
var file = path.join(dir, filename);
fs.stat(file, function(err, stat) {
if (err) return done(err);
if (err && options.doneOnErr === true) return done(err);
if (stat && stat.isDirectory()) {
if (options.recursive) {
if (options.matchDir && !matches(filename, options.matchDir)) return next();
if (options.excludeDir && matches(filename, options.excludeDir)) return next();
readFiles(file, options, callback, function(err, sfiles) {
if (err) return done(err);
if (err && options.doneOnErr === true) return done(err);
files = files.concat(sfiles);
next();
});
Expand All @@ -98,7 +101,9 @@ function readFiles(dir, options, callback, complete) {
fs.readFile(file, options.encoding, function(err, data) {
if (err) {
if (err.code === 'EACCES') return next();
return done(err);
if (options.doneOnErr === true) {
return done(err);
}
}
if (callback.length > 3)
if (options.shortName) callback(null, data, filename, next);
Expand Down
23 changes: 17 additions & 6 deletions lib/readfilesstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function readFilesStream(dir, options, callback, complete) {
};
options = extend({
recursive: true,
encoding: 'utf8'
encoding: 'utf8',
doneOnErr: true
}, options);
var files = [];

Expand All @@ -70,7 +71,12 @@ function readFilesStream(dir, options, callback, complete) {
};

fs.readdir(dir, function(err, list) {
if (err) return done(err);
if (err)  {
if (options.doneOnErr === true) {
if (err.code === 'EACCES') return done();
return done(err);
}
}
var i = 0;

if (options.reverse === true ||
Expand All @@ -84,32 +90,37 @@ function readFilesStream(dir, options, callback, complete) {
if (!filename) return done(null, files);
var file = path.join(dir, filename);
fs.stat(file, function(err, stat) {
if (err) return done(err);
if (err && options.doneOnErr === true) return done(err);
if (stat && stat.isDirectory()) {
if (options.recursive) {
if (options.matchDir && !matches(filename, options.matchDir)) return next();
if (options.excludeDir && matches(filename, options.excludeDir)) return next();
readFilesStream(file, options, callback, function(err, sfiles) {
if (err) return done(err);
if (err && options.doneOnErr === true) return done(err);
files = files.concat(sfiles);
next();
});
} else next();
} else {
} else if (stat && stat.isFile()) {
if (options.match && !matches(filename, options.match)) return next();
if (options.exclude && matches(filename, options.exclude)) return next();
if (options.filter && !options.filter(filename)) return next();
if (options.shortName) files.push(filename);
else files.push(file);
var stream = fs.createReadStream(file);
stream.setEncoding(options.encoding);
stream.on('error',function(err) {
return done(err);
if (options.doneOnErr === true) return done(err);
next();
});
if (callback.length > 3)
if (options.shortName) callback(null, stream, filename, next);
else callback(null, stream, file, next);
else callback(null, stream, next);
}
else {
next();
}
});
})();

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/testdir3/broken_link.txt
3 changes: 3 additions & 0 deletions test/fixtures/testdir3/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
begin content of file1
content body
end content of file1
73 changes: 72 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
dir = require('..'),
fixturesDir = path.join(__dirname, 'fixtures'),
tdir = path.join(fixturesDir, 'testdir'),
tdir2 = path.join(fixturesDir, 'testdir2');
tdir2 = path.join(fixturesDir, 'testdir2'),
tdir3 = path.join(fixturesDir, 'testdir3');

describe('readfiles method', function() {

Expand Down Expand Up @@ -435,6 +436,33 @@ describe('readfiles method', function() {
});
});

it('should done on error', function(done) {
dir.readFiles(
tdir3, function(err, content, filename, next) {
should.not.exist(err);
var shortName = path.basename(filename).replace(new RegExp(path.extname(filename) + '$'), '');
var expected = 'begin content of ' + shortName + '\ncontent body\nend content of ' + shortName;
content.replace(/\r/g, '').should.equal(expected);
next();
}, function(err) {
should.exist(err);
done();
});
});

it('if given doneOnErr to false, should not done on error', function(done) {
dir.readFiles(
tdir3, { doneOnErr: false },function(err, content, filename, next) {
should.not.exist(err);
var shortName = path.basename(filename).replace(new RegExp(path.extname(filename) + '$'), '');
var expected = 'begin content of ' + shortName + '\ncontent body\nend content of ' + shortName;
content.replace(/\r/g, '').should.equal(expected);
next();
}, function() {
done();
});
});

it('can be called with a callback in which the filename argument is omitted', function(done) {
dir.readFiles(
tdir, function(err, content, next) {
Expand Down Expand Up @@ -1018,6 +1046,49 @@ describe('readfilesstream method', function() {
});
});

it('should done on error', function(done) {
dir.readFilesStream(
tdir3, function(err, stream, filename, next) {
should.not.exist(err);
var content = '';
stream.on('data',function(buffer) {
var part = buffer.toString();
content += part;
});
stream.on('end',function() {
content = content.replace(/\r/g, '');
var shortName = path.basename(filename).replace(new RegExp(path.extname(filename) + '$'), '');
var expected = 'begin content of ' + shortName + '\ncontent body\nend content of ' + shortName;
content.should.equal(expected);
filenames.push(shortName);
next();
});
}, function(err) {
should.exist(err);
done();
});
});

it('if given doneOnErr to false, should not done on error', function(done) {
dir.readFilesStream(
tdir3, { doneOnErr: false },function(err, stream, filename, next) {
should.not.exist(err);
var shortName = path.basename(filename).replace(new RegExp(path.extname(filename) + '$'), '');
var expected = 'begin content of ' + shortName + '\ncontent body\nend content of ' + shortName;
var content = '';
stream.on('data',function(buffer) {
var part = buffer.toString();
content += part;
});
stream.on('end',function() {
content.replace(/\r/g, '').should.equal(expected);
next();
});
}, function() {
done();
});
});

it('can be called with a callback in which the filename argument is omitted', function(done) {
dir.readFilesStream(
tdir, function(err, stream, next) {
Expand Down

0 comments on commit 2ab0c06

Please sign in to comment.