npm install filetrek
var filetrek = require("filetrek");
var p = filetrek('./', function(name, stats, root){
//do stuff
});
p.then(function(info){
//info is an Array
//info = [{name: "filename", stats: object, root: "root directory"}]
});
var filetrek = require("filetrek"),
options = {
ignore: ["*script.js", /\mark.xml$/, "filename.html"],
find: ["*.js", /\.xml$/, "filename.html"]
};
var p = filetrek('./', options, function(name, stats, root, sub){
//do stuff
});
p.then(function(info){
//info is an Array
//info = [{name: "filename", stats: object, root: "root directory"}]
});
options.find is used before options.ignore.
The find, and ignore options should be arrays of glob strings, instances of RegExp, or plain strings.
The glob string patterns are the same used in the minimatch module.
Plain strings will match up to the end of file names.
Returns all matched files.
Excludes matched files in the return.
The name of the file, or directory.
A stats object as returned from fs.stats
.
The original directory to be scanned.
The whole name of the file path not including the root.
var files = [],
filetrek = require("filetrek");
function walkFiles(folder){
return filetrek(folder, function(name, stats, root, sub){
if(stats.isDirectory()){
//The directory path is used to continue walking files.
return path.join(root, name);
}
//Work with the files.
});
}
walkFiles('./').then(function(info){
console.log('done');
});
You can also return a promise in the callback like this:
function walkFiles(folder){
return filetrek(folder, function(name, stats, root){
return promiseFactory();
});
}
walkFiles('./').then(function(resultsOfAllPromiseFactoryPromises){});
The promises returned like this are passed to Promise.all.
Promises are done in series so operation order is preserved.
If there are other operations on the file name you want to do, and that operation returns a promise returning a promise in the callback might be what you want to do.
All values that aren't strings, or promises returned in the callback are no-ops.
Strings returned in the callback that aren't a directory will cause an error.
Walk directory contents.
filetrek returns a promise. After all files are walked the promise resolves.