fs-readdirp
v1.0.0
Published
simple readdirp with filter and sync/async version
Downloads
5
Maintainers
Readme
simple readdirp with filter and sync/async version
Features
- just traverse the file path, not the content of file
- readdirpSync for sync usecase
Usage
readdirpSync(dir, [filter])
sync to read the dir. the result is an array, which contains the file path(no filter param) or the filter return
dir
: the start directory, which will not include in the resultfilter
: the filter functionfilter(filePath, stats)
.filePath
is the full path for the sub dir or file.stats
is the lstat return- the
return
value will be pushed at the result. if you returnfalse
, then this file will be ignored
readdirp(dir, filter, done)
async to read the dir.
filter
:filter(filePath, stats, [callback])
.callback
:callback(err, ret)
. if no callback, then thereturn
will be used in the result. otherwise, callback'sret
parameter will be used in the result. if theret
param's value is false, the file will be ignored
done
: when finished, done is called.done(err, results)
var readdirp = require('fs-readdirp').readdirp;
var readdirpSync = require('fs-readdirp').readdirpSync;
//read current directory's sub file path(absolute path)
var files = readdirpSync('.');
//read current directory's sub file relative path
var files = readdirpSync('.', function(filePath, stats){
return require('path').relative('.', filePath);
});
//ignore all directory, only its files
var files = readdirpSync('.', function(filePath, stats){
if(stats.isDirectory()) return false;
return require('path').relative('.', filePath);
});
//async with no-callback filter: filter is sync
readdirp('.', function(filePath, stats){
if(stats.isDirectory()) return false;
return require('path').relative('.', filePath);
}, function(err, files){
});
//async with callback filter: filter is async
readdirp('.', function(filePath, stats, cb){
if(stats.isDirectory()) cb(null, false);
else cb(null, require('path').relative('.', filePath));
}, function(err, files){
});
License :
Licensed under MIT
Copyright (c) 2015 kiliwalk