node-klaw-sync
klaw-sync
is a Node.js recursive and fast file system walker, which is the synchronous counterpart of klaw. It lists all files and directories inside a directory recursively and returns an array of objects that each object has two properties: path
and stats
. path
is the full path of the file or directory and stats
is an instance of fs.Stats.
Install
npm i klaw-sync
Usage
klawSync(directory[, options])
directory
<String>
options
<Object>
(optional) all options arefalse
by defaultnodir
<Boolean>
- return only files (ignore directories).
nofile
<Boolean>
- return only directories (ignore files).
depthLimit
:<Number>
- the number of times to recurse before stopping.
-1
for unlimited.
- the number of times to recurse before stopping.
fs
:<Object>
- custom
fs
, useful when mockingfs
object.
- custom
filter
<Function>
- function that gets one argument
fn({path: '', stats: {}})
and returns true to include or false to exclude the item.
- function that gets one argument
traverseAll
<Boolean>
- traverse all subdirectories, regardless of
filter
option. (When set totrue
,traverseAll
produces similar behavior to the default behavior prior to v4.0.0. The current default oftraverseAll: false
is equivalent to the oldnoRecurseOnFailedFilter: true
).
- traverse all subdirectories, regardless of
- Return:
<Array<Object>>
[{path: '', stats: {}}]
Examples
const klawSync = const paths = // paths = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/file1', stats: {}}]
catch error
const klawSync = let pathstry paths = catch er consoleconsoledirpaths
files only
const klawSync = const files = // files = [{path: '/some/dir/file1', stats: {}}, {path: '/some/dir/file2', stats: {}}]
directories only
const klawSync = const dirs = // dirs = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/dir2', stats: {}}]
ignore hidden directories
const path = const klawSync = const filterFn = { const basename = path return basename === '.' || basename0 !== '.'} const paths =
filter based on stats
Here traverseAll
option is required since we still want to read all directories even if they don't pass the filter
function, to see if their contents do pass the filter
function.
const klawSync = const refTime = 2017 3 24const filterFn = itemstatsmtime > refTime const paths =
Run tests
lint: npm run lint
unit test: npm run unit
lint & unit: npm test
benchmark: npm run benchmark
Performance compare to other similar modules
Running some benchmark tests on these modules:
klaw-sync
- walk-sync
(as of Jan 25, 2017) klaw-sync
is the fastest module!
results (tested on Ubuntu 18.04, Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz, 8 CPUs, 8g RAM, node v10.9.0)
Running benchmark tests.. root dir length: 1110walk-sync x 139 ops/sec ±2.48% klaw-sync x 163 ops/sec ±1.20% Fastest is klaw-sync root dir length: 11110walk-sync x 13.23 ops/sec ±1.10% klaw-sync x 15.10 ops/sec ±1.06% Fastest is klaw-sync root dir length: 111110walk-sync x 1.17 ops/sec ±2.06% klaw-sync x 1.25 ops/sec ±2.10% Fastest is klaw-sync
Credit
Special thanks to:
for their contribution and support.
License
Licensed under MIT