Last active
January 14, 2017 16:25
-
-
Save maggiesavovska/dd998b47f94af1fd30976666bd171a22 to your computer and use it in GitHub Desktop.
node.js recursive function to read all files in directory (searches through all directories to find files)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//******************************************************/ | |
//Include all files going one directory deep | |
//* @param {String} d - directory name of directory where search is occurring | |
//* @param {Array || String} exclude - array of file names {STRING} to skip | |
//******************************************************/ | |
function fileSearch(d, exclude){ | |
var exclude = exclude || []; | |
//in case string was passed instead of array | |
if (typeof exclude == "String") exclude = exclude.split(','); | |
var fs = require("fs"); | |
var path = require("path"); | |
//determine if directory contains index.js file | |
function indexExists(dir){ | |
console.log('in indexexists', dir); | |
return new Promise(function(resolve, reject){ | |
fs.readdir(dir, function(err, files) { | |
console.log(files); | |
files.find(function(file){ | |
if(/^index\.js$/.test(file)){ | |
return resolve('skip'); | |
} | |
}); | |
resolve('done'); | |
}); | |
}); | |
}; | |
//recursively search through directories and require files | |
function searchdir(file_or_dir) { | |
console.log('in searchdir', file_or_dir); | |
indexExists(file_or_dir).then(function(result){ | |
console.log('result', result); | |
//if this directory has it's own index.js file then skip it | |
if(result == 'skip') return false; | |
//array of all files in directory | |
var fd = fs.readdirSync(file_or_dir); | |
//iterate through files | |
for(var q = 0; q < fd.length; q++){ | |
var item = fd[q]; | |
var p = path.join(file_or_dir, item); | |
if (fs.lstatSync(p).isDirectory()) { | |
searchdir(p); | |
} else { | |
//exclude any files passed in exclude array | |
var skip = false; | |
//exclude any files passed in exclude array | |
for(var z = 0; z < exclude.length; z++){ | |
var ex = exclude[z]; | |
if(item == ex){ | |
skip = true; | |
} | |
} | |
if(skip) { | |
continue; | |
} | |
console.log(p); | |
require(p); | |
} | |
};//end for loop | |
}); | |
}; | |
var fd = fs.readdirSync(d); | |
console.log(fd.length); | |
for(var i = 0; i < fd.length; i++){ | |
console.log(i); | |
if(i > 4) break; | |
var item = fd[i]; | |
//default exclude | |
if(item == ".DS_Store" || item == ".git" || item == "index.js" || item == "test.js"){ | |
continue; | |
} | |
var p = path.join(d, item); | |
if(fs.lstatSync(item).isDirectory()){ | |
searchdir(p); | |
}else{ | |
//exclude any files passed in exclude array | |
var skip; | |
for(var m = 0; m < exclude.length; m++){ | |
var ex = exclude[i]; | |
if(item == ex){ | |
skip = true; | |
} | |
} | |
if(skip) continue; | |
console.log(p); | |
require(p); | |
} | |
};//end for files and directories | |
}; | |
var d = __dirname; | |
fileSearch(d, ['skip-this-file.es6']); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Useful for webpack