Comparing version 0.0.4 to 0.0.7
10
index.js
@@ -1,5 +0,4 @@ | ||
var path = require('path') | ||
, dirpaths = require(path.join(__dirname + '/lib/paths')) | ||
, key | ||
; | ||
var key, | ||
path = require('path'), | ||
dirpaths = require(__dirname + '/lib/paths'); | ||
@@ -10,3 +9,2 @@ for (key in dirpaths) { | ||
exports.readFiles = require(path.join(__dirname + '/lib/readfiles')); | ||
exports.readFiles = require(path.join(__dirname + '/lib/readfiles')); |
@@ -57,3 +57,4 @@ | ||
if (err) return done(err); | ||
callback(null, data, file, next); | ||
if (callback.length > 3) callback(null, data, file, next); | ||
else callback(null, data, next); | ||
}); | ||
@@ -60,0 +61,0 @@ } |
{ | ||
"name" : "node-dir", | ||
"version" : "0.0.4", | ||
"version" : "0.0.7", | ||
"description" : "asynchronous file and directory operations for Node.js", | ||
@@ -5,0 +5,0 @@ "main" : "index", |
114
README.md
# node-dir | ||
A small node.js module to provide some convenience methods for asynchronous, non-blocking recursive directory operations,including methods to get an array of all files, subdirectories, or both (with the option to either combine or separate the results), and a method for sequentially reading and processing the contents all files in a directory recursively, optionally firing a callback when finished. | ||
A small node.js module to provide some convenience methods for asynchronous, non-blocking, recursive directory operations like being able to get an array of all files, subdirectories, or both (with the option to either combine or separate the results), and for sequentially reading and processing the contents all files in a directory recursively, optionally firing a callback when finished. | ||
### methods | ||
for the sake of brevity, assume that the following line of code precedes all of the examples | ||
#### methods | ||
For the sake of brevity, assume that the following line of code precedes all of the examples. | ||
var dir = require('node-dir'); | ||
```javascript | ||
var dir = require('node-dir'); | ||
``` | ||
and that callbacks would typically have a first line of code something like | ||
#### readFiles( dir, fileCallback, [finishedCallback] ) | ||
Sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. | ||
if (err) throw err; | ||
#### readFiles( dir, fileCallback, finishedCallback ) | ||
sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. | ||
```javascript | ||
dir.readFiles(__dirname, | ||
function(err, content, next) { | ||
if (err) throw err; | ||
console.log('content for', filepath + '\n' + content); | ||
// continue to next file: | ||
next(); | ||
}, | ||
function(err, files){ | ||
if (err) throw err; | ||
console.log('finished reading files:',files); | ||
}); | ||
dir.readFiles(__dirname, | ||
function(err, content, filepath, next) { | ||
console.log('filepath:', filepath + '\n' + 'content:', content); | ||
next(); | ||
}, | ||
function(err, files){ | ||
console.log('finished reading files:', files); | ||
}); | ||
// the callback for each file can optionally have a filename argument as its 3rd parameter | ||
// and the finishedCallback argument is optional, e.g. | ||
dir.readFiles(__dirname, function(err, content, filename, next) { | ||
console.log('processing content of file', filename); | ||
next(); | ||
}); | ||
``` | ||
#### files( dir, callback ) | ||
asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback | ||
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback. | ||
```javascript | ||
dir.files(__dirname, function(err, files) { | ||
if (err) throw err; | ||
console.log(files); | ||
}); | ||
``` | ||
dir.files(__dirname, function(err, files) { | ||
console.log(files); | ||
}); | ||
#### subdirs( dir, callback ) | ||
asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of directory paths to a callback | ||
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of directory paths to a callback. | ||
dir.subdirs(__dirname, function(err, subdirs) { | ||
console.log(subdirs); | ||
}); | ||
```javascript | ||
dir.subdirs(__dirname, function(err, subdirs) { | ||
if (err) throw err; | ||
console.log(subdirs); | ||
}); | ||
``` | ||
#### paths(dir, [combine], callback ) | ||
asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of both file and directory paths to a callback | ||
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of both file and directory paths to a callback. | ||
###### separated into two distinct arrays (paths.files and paths.dirs) | ||
Separated into two distinct arrays (paths.files and paths.dirs) | ||
dir.paths(__dirname, function(err, paths) { | ||
console.log('files:\n',paths.files); | ||
console.log('subdirs:\n', paths.dirs); | ||
}); | ||
###### combined in a single array (convenience method for concatenation of the above) | ||
```javascript | ||
dir.paths(__dirname, function(err, paths) { | ||
if (err) throw err; | ||
console.log('files:\n',paths.files); | ||
console.log('subdirs:\n', paths.dirs); | ||
}); | ||
``` | ||
dir.paths(__dirname, true, function(err, paths) { | ||
console.log('paths:\n',paths); | ||
}); | ||
### tests | ||
to run the tests, you will need mocha, chai, and should.js installed, preferably globally, in which case you can run the tests by simply typing | ||
mocha | ||
Combined in a single array (convenience method for concatenation of the above) | ||
```javascript | ||
dir.paths(__dirname, true, function(err, paths) { | ||
if (err) throw err; | ||
console.log('paths:\n',paths); | ||
}); | ||
``` | ||
### License | ||
MIT-Style licensed. See LICENSE.txt. | ||
#### contributors | ||
- [Nathan Cartwright](https://github.com/fshost) | ||
- [robatron](https://github.com/robatron) | ||
- [nazomikan](https://github.com/nazomikan) | ||
## License | ||
MIT licensed (See LICENSE.txt) |
@@ -38,2 +38,27 @@ var path = require('path'); | ||
}); | ||
it('can be called with a callback in which the filename argument is omitted', function(done) { | ||
dir.readFiles( | ||
tdir, function(err, content, next) { | ||
expect(err).to.equal(null); | ||
content.should.be.a('string'); | ||
content.indexOf('begin content of').should.equal(0); | ||
next(); | ||
}, function(err) { | ||
expect(err).to.equal(null); | ||
done(); | ||
}); | ||
}); | ||
it('can be called with the done callback argument omitted', function(done) { | ||
var i = 0; | ||
dir.readFiles( | ||
tdir, function(err, content, next) { | ||
expect(err).to.equal(null); | ||
next(); | ||
i++; | ||
if (i === 4) done(); | ||
}); | ||
}); | ||
}); | ||
@@ -89,6 +114,6 @@ | ||
relFiles.should.eql([ | ||
'testdir/file1.txt', | ||
'testdir/file2.txt', | ||
'testdir/subdir/file3.txt', | ||
'testdir/subdir/file4.txt' | ||
'testdir/file1.txt', | ||
'testdir/file2.txt', | ||
'testdir/subdir/file3.txt', | ||
'testdir/subdir/file4.txt' | ||
]); | ||
@@ -110,7 +135,7 @@ relPaths.length.should.equal(1); | ||
relPaths.should.eql([ | ||
'testdir/file1.txt', | ||
'testdir/file2.txt', | ||
'testdir/subdir/file3.txt', | ||
'testdir/subdir/file4.txt', | ||
'testdir/subdir' | ||
'testdir/file1.txt', | ||
'testdir/file2.txt', | ||
'testdir/subdir/file3.txt', | ||
'testdir/subdir/file4.txt', | ||
'testdir/subdir' | ||
]); | ||
@@ -117,0 +142,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
16353
324
89