Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ node_modules

# Users Environment Variables
.lock-wscript

.vscode
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ var recursiveReadSync = require('recursive-readdir-sync')

try {
files = recursiveReadSync('/your/path/here');

// optional
files = recursiveReadSync('/your/path/here', ['excluded folder1','excluded folder2']);

} catch(err){
if(err.errno === 34){
console.log('Path does not exist');
Expand Down
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ var fs = require('fs')
;

// how to know when you are done?
function recursiveReaddirSync(path) {
function recursiveReaddirSync(path, excludeFolders) {
var list = []
, files = fs.readdirSync(path)
, stats
;

files.forEach(function (file) {
stats = fs.lstatSync(p.join(path, file));
if(stats.isDirectory()) {
list = list.concat(recursiveReaddirSync(p.join(path, file)));
if (stats.isDirectory()) {
if (IsArray(excludeFolders) && isObjInArray(file, excludeFolders))
return;
list = list.concat(recursiveReaddirSync(p.join(path, file), excludeFolders));
} else {
list.push(p.join(path, file));
}
Expand All @@ -21,4 +23,15 @@ function recursiveReaddirSync(path) {
return list;
}

function IsArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}

function isObjInArray(obj, array) {
var index = array.indexOf(obj);
if (index === -1)
return false;
return true;
}

module.exports = recursiveReaddirSync;
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Functionality testing.', function(){
return path.resolve(__dirname, f);
});

var results = recursiveReaddirSync(__dirname + '/nested');
var results = recursiveReaddirSync(__dirname + '/nested',['exclude folder']);

it('should return an array with length equal to that of expectedFiles', function(){
expect(results).to.have.length(expectedFiles.length);
Expand Down
Empty file.