node-readfiles
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -58,4 +58,2 @@ var fs = require('fs'); | ||
(function next() { | ||
var cbNext = null; | ||
if (options.async === true) cbNext = next; | ||
@@ -82,4 +80,9 @@ // if the file list is empty then call done | ||
// call callback with the error | ||
callback(err, relFilename, null, stat, cbNext); | ||
var result = callback(err, relFilename, null, stat); | ||
// if callback result is a function then call the result with next as a parameter | ||
if (typeof result === 'function' && !err) { | ||
return result(next); | ||
} | ||
// if rejectOnError is not false, reject the promise | ||
@@ -90,4 +93,3 @@ if (options.rejectOnError !== false) { | ||
//if async is true then don't call next | ||
return options.async !== true && next(); | ||
return next(); | ||
} | ||
@@ -104,3 +106,3 @@ | ||
subdirs.push(filename); | ||
traverseDir(fullpath, function (err, count, files) { | ||
traverseDir(fullpath, function () { | ||
subdirs.pop(); | ||
@@ -127,3 +129,3 @@ next(); | ||
// promise to handle file reading (if not disabled) | ||
new Promise(function (resolve, reject) { | ||
new Promise(function (resolve) { | ||
if (options.readContents === false) { | ||
@@ -139,3 +141,8 @@ return resolve(null); | ||
// call the callback with the content | ||
callback(err, outputName, content, stat, cbNext); | ||
var result = callback(err, outputName, content, stat); | ||
// if callback result is a function then call the result with next as a parameter | ||
if (typeof result === 'function' && !err) { | ||
return result(next); | ||
} | ||
// call the next if async is not true | ||
@@ -148,4 +155,3 @@ options.async !== true && next(); | ||
//if async is true then don't call next | ||
options.async !== true && next(); | ||
next(); | ||
}); | ||
@@ -152,0 +158,0 @@ |
{ | ||
"name": "node-readfiles", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A lightweight Node.js module to recursively read files in a directory using ES6 Promises", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"start": "echo 'Not a runnable module' && exit 1", | ||
"test": "mocha", | ||
"lint": "eslint lib test", | ||
"preversion": "npm run lint && npm test" | ||
}, | ||
@@ -27,4 +30,6 @@ "keywords": [ | ||
"chai": "^3.5.0", | ||
"eslint": "^2.10.2", | ||
"mocha": "^2.4.5", | ||
"mock-fs": "^3.9.0" | ||
"mock-fs": "^3.9.0", | ||
"sinon": "^1.17.4" | ||
}, | ||
@@ -31,0 +36,0 @@ "dependencies": { |
@@ -37,14 +37,27 @@ # node-readfiles | ||
* **hidden**: a boolean value wether to exclude hidden files prefixed with a `.` (defaults to true) | ||
* **async**: a boolean value which enables/disables asynchronous traversal of the tree. When set to `true`, the `next()` in the `callback` must be called. (defaults to false) | ||
### callback(err, filename, content, stat, next) | ||
### callback(err, filename, content, stat) | ||
The optional callback function is triggered everytime a file is found. If there's an error while reading the file the `err` parameter will contain the error that occured, When `readContents` is true, the `contents` parameter will be populated with the contents of the file encoded using the `encoding` option. For convenience the `stat` result object is passed to the callback for you to use. | ||
When working with asynchronous operations, you can set the `async` to `true`. This will enabled you to continue traversal of the directory when you call `next()`. See bellow for an example. | ||
<span id="read-files">[1]</span> The `contents` parameter will be `null` when the `readContents` option is `false`. | ||
<span id="read-files">[1]</span> The `contents` parameter will be `null` when the `readContents` option is `false`. | ||
##### Asynchronous Callback | ||
When working with asynchronous operations, you can simply return a `function (next) { ... }` which will enabled you to completed your asynchronous operation until you call `next()`. | ||
```javascript | ||
readfiles('/path/to/dir/', function (err, content, filename, stat) { | ||
if (err) throw err; | ||
return function (next) { | ||
setTimeout(function () { | ||
console.log('File ' + filename); | ||
next(); | ||
}, 3000); | ||
}; | ||
}); | ||
``` | ||
### _Promise(files)_ | ||
@@ -156,19 +169,3 @@ | ||
When making asynchronous calls on files, you can enable asynchronous support by setting the `async` to `true`. | ||
```javascript | ||
readfiles('/path/to/dir/', { | ||
async: true | ||
}, function (err, content, filename, stat, next) { | ||
if (err) throw err; | ||
setTimeout(function () { | ||
console.log('File ' + filename); | ||
next(); | ||
}, 3000); | ||
return false; | ||
}); | ||
``` | ||
## License | ||
MIT licensed (See LICENSE.txt) |
var mock = require('mock-fs'); | ||
module.exports = { | ||
"/path/to/dir": { | ||
".system": "SYSTEM", | ||
"def.dat": "DEF", | ||
"abc.txt": "ABC", | ||
"error-file.dat": mock.symlink({path:'/fake/path/file'}), | ||
"subdir": { | ||
".dot": "DOT", | ||
"test456.dat": "456", | ||
"test789.txt": "789", | ||
"test123.txt": "123", | ||
"subsubdir": { | ||
".hidden": "HIDDEN", | ||
"abc123.txt": "123", | ||
"def456.dat": "456" | ||
'/path/to/dir': { | ||
'.system': 'SYSTEM', | ||
'def.dat': 'DEF', | ||
'abc.txt': 'ABC', | ||
'error-file.dat': mock.symlink({path:'/fake/path/file'}), | ||
'subdir': { | ||
'.dot': 'DOT', | ||
'test456.dat': '456', | ||
'test789.txt': '789', | ||
'test123.txt': '123', | ||
'subsubdir': { | ||
'.hidden': 'HIDDEN', | ||
'abc123.txt': '123', | ||
'def456.dat': '456' | ||
} | ||
}, | ||
"otherdir": { | ||
".other": "DOT", | ||
"error-file.dat": mock.symlink({path:'/fake/path/file'}), | ||
"test789.txt": "789", | ||
"test123.txt": "123", | ||
"subsubdir": { | ||
".hidden": "HIDDEN", | ||
"abc123.txt": "123", | ||
"def456.dat": "456" | ||
'otherdir': { | ||
'.other': 'DOT', | ||
'error-file.dat': mock.symlink({path:'/fake/path/file'}), | ||
'test789.txt': '789', | ||
'test123.txt': '123', | ||
'subsubdir': { | ||
'.hidden': 'HIDDEN', | ||
'abc123.txt': '123', | ||
'def456.dat': '456' | ||
} | ||
} | ||
} | ||
} | ||
}; |
var mock = require('mock-fs'); | ||
module.exports = { | ||
"/path/to/dir": { | ||
".system": "SYSTEM", | ||
"def.dat": "DEF", | ||
"abc.txt": "ABC", | ||
"abc123.txt": "ABC123", | ||
"subdir": { | ||
".dot": "DOT", | ||
"test456.dat": "456", | ||
"test789.txt": "789", | ||
"test123.txt": "123", | ||
"abc123.txt": "ABC123", | ||
"subsubdir": { | ||
".hidden": "HIDDEN", | ||
"abc123.dat": "ABC123", | ||
"def456.dat": "456" | ||
'/path/to/dir': { | ||
'.system': 'SYSTEM', | ||
'def.dat': 'DEF', | ||
'abc.txt': 'ABC', | ||
'abc123.txt': 'ABC123', | ||
'subdir': { | ||
'.dot': 'DOT', | ||
'test456.dat': '456', | ||
'test789.txt': '789', | ||
'test123.txt': '123', | ||
'abc123.txt': 'ABC123', | ||
'subsubdir': { | ||
'.hidden': 'HIDDEN', | ||
'abc123.dat': 'ABC123', | ||
'def456.dat': '456' | ||
} | ||
}, | ||
"otherdir": { | ||
".other": "DOT", | ||
"symlink.dat": mock.symlink({path:'/path/to/dir/subdir/test123.txt'}), | ||
"test789.txt": "789", | ||
"test123.txt": "123", | ||
"subsubdir": { | ||
".hidden": "HIDDEN", | ||
"abc123.txt": "ABC123", | ||
"def456.txt": "456" | ||
'otherdir': { | ||
'.other': 'DOT', | ||
'symlink.dat': mock.symlink({path:'/path/to/dir/subdir/test123.txt'}), | ||
'test789.txt': '789', | ||
'test123.txt': '123', | ||
'subsubdir': { | ||
'.hidden': 'HIDDEN', | ||
'abc123.txt': 'ABC123', | ||
'def456.txt': '456' | ||
} | ||
} | ||
} | ||
} | ||
}; |
module.exports = { | ||
"/path/to/dir": { | ||
"abc.txt": "ABC", | ||
"def.dat": "DEF", | ||
"test123.txt": "123", | ||
"test456.dat": "456" | ||
'/path/to/dir': { | ||
'abc.txt': 'ABC', | ||
'def.dat': 'DEF', | ||
'test123.txt': '123', | ||
'test456.dat': '456' | ||
} | ||
} | ||
}; |
var expect = require('chai').expect; | ||
var sinon = require('sinon'); | ||
var mock = require('mock-fs'); | ||
@@ -16,6 +17,13 @@ var readfiles = require('../lib/readfiles'); | ||
describe('defaults', function () { | ||
var clock; | ||
beforeEach(function () { | ||
clock = sinon.useFakeTimers(); | ||
mock(fixtures.flat); | ||
}); | ||
afterEach(function () { | ||
clock.restore(); | ||
}); | ||
it('is called with the relative filename and the contents of each file', function(done) { | ||
@@ -28,2 +36,4 @@ var files = ['abc.txt', 'def.dat', 'test123.txt', 'test456.dat']; | ||
if (files.length === 0) done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -39,3 +49,6 @@ }); | ||
readfiles('/path/to/dir', function (err, filename, content) { | ||
expect(content).to.be.null; | ||
expect(err.message).to.equal('ENOENT, no such file or directory \'/path/to/dir/badfile.txt\''); | ||
}).catch(function (err) { | ||
expect(err.message).to.equal('ENOENT, no such file or directory \'/path/to/dir/badfile.txt\''); | ||
done(); | ||
@@ -49,2 +62,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -59,2 +74,26 @@ }); | ||
}); | ||
it('callback waits for an asynchronous process when returning a function in callback', function(done) { | ||
var count = 0; | ||
var expectFiles = [ | ||
'abc.txt', | ||
'def.dat', | ||
'test123.txt', | ||
'test456.dat' | ||
]; | ||
readfiles('/path/to/dir', function (err, filename) { | ||
return function (next) { | ||
expect(filename).to.equal(expectFiles[count++]); | ||
setTimeout(function () { | ||
next(); | ||
}, 1000); | ||
clock.tick(1000); | ||
}; | ||
}).then(function (files) { | ||
expect(files).to.deep.equal(expectFiles); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
@@ -89,2 +128,4 @@ | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -110,3 +151,3 @@ }); | ||
'/path/to/dir/subdir/test789.txt' | ||
] | ||
]; | ||
readfiles('/path/to/dir', { | ||
@@ -119,2 +160,4 @@ filenameFormat: readfiles.FULL_PATH | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -140,3 +183,3 @@ }); | ||
'subdir/test789.txt' | ||
] | ||
]; | ||
readfiles('/path/to/dir', { | ||
@@ -149,2 +192,4 @@ filenameFormat: readfiles.RELATIVE | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -178,16 +223,24 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
}); | ||
it('does not call done when one file throws an error and \'rejectOnError\' is false', function(done) { | ||
it('does not stop reading files when one file throws an error and \'rejectOnError\' is false', function(done) { | ||
mock(fixtures.deepError); | ||
fileCount = 0; | ||
var fileCount = 0; | ||
readfiles('/path/to/dir', { | ||
rejectOnError: false | ||
}, function (err, filename) { | ||
}, function (err) { | ||
if (err) { | ||
expect(err.message).to.have.string('ENOENT, no such file or directory'); | ||
} | ||
fileCount++; | ||
}).then(function (files) { | ||
expect(fileCount).to.equal(13); | ||
expect(files.length).to.equal(11); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -197,2 +250,3 @@ }); | ||
it('callback does not return the file contents when \'readContents\' is false', function(done) { | ||
var fileCount = 0; | ||
readfiles('/path/to/dir', { | ||
@@ -202,4 +256,9 @@ readContents: false | ||
expect(contents).to.be.null; | ||
fileCount++; | ||
}).then(function (files) { | ||
expect(fileCount).to.equal(14); | ||
expect(files.length).to.equal(14); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -209,3 +268,2 @@ }); | ||
it('callback returns file contents encoded in the specified \'encoding\' type', function(done) { | ||
var count = 0; | ||
var expectFiles = { | ||
@@ -233,3 +291,6 @@ 'abc.txt': 'ABC', | ||
}).then(function (files) { | ||
expect(files.length).to.equal(14); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -259,2 +320,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -293,2 +356,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -312,2 +377,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -338,2 +405,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -352,2 +421,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -373,2 +444,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -388,2 +461,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -401,2 +476,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -423,2 +500,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -437,2 +516,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -458,2 +539,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -477,2 +560,4 @@ }); | ||
done(); | ||
}).catch(function (err) { | ||
done(err); | ||
}); | ||
@@ -479,0 +564,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
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
31186
11
709
5
170