Node.js: klaw-sync
klaw-sync
is a recursive 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 install klaw-sync
Usage
klawSync(directory[, options])
-
directory
{String}
-
options
{Object}
optional (all options are false
by default)
-
ignore
{String | Array<String>}
any paths or minimatch patterns to ignore (can be string or an array of strings)
-
nodir
{Boolean}
return only files (ignore directories)
-
nofile
{Boolean}
return only directories (ignore files)
-
return: {Array<Object>}
[{path: '', stats: {}}]
Examples
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir')
catch error
var klawSync = require('klaw-sync')
var paths
try {
paths = klawSync('/some/dir')
} catch (er) {
console.error(er)
}
console.log(paths)
files only
var klawSync = require('klaw-sync')
var files = klawSync('/some/dir', {nodir: true})
directories only
var klawSync = require('klaw-sync')
var dirs = klawSync('/some/dir', {nofile: true})
ignore node_modules
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: 'node_modules'})
ignore node_modules
and .git
using minimatch patterns
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: '{node_modules,.git}'})
ignore node_modules
, .git
and all *.js
files minimatch patterns
var klawSync = require('klaw-sync')
var paths = klawSync('/some/dir', {ignore: ['{node_modules,.git}', '*.js']})
Run tests
lint: npm run lint
unit test: npm run unit
lint & unit: npm test
Performance comparison to other similar modules
Sometimes it's fun to run speed tests on similar functions or modules. The bm.js
runs some basic benchmark tests for two cases, without --ignore
(basic usage) and with --ignore
, on these modules:
All of these modules are great. I appreciate the works. I've personally learned a lot from them.
Just for fun, it turned out for the most cases klaw-sync
is faster than other modules!
#####run benchmark (performance)
basic usage without anything to ignore
npm run benchmark -- --dir=/some/dir -p
one item to ignore
npm run benchmark -- --dir=/some/dir -p -i "node_modules"
multiple items to ignore
npm run benchmark -- --dir=/some/dir -p -i "node_modules" -i "*.js"
#####run benchmark (exec time)
npm run benchmark -- --dir=/some/dir -t
npm run benchmark -- --dir=/some/dir -t -i ".git"
npm run benchmark -- --dir=/some/dir -t -i ".git" -i "*.js"
Credit
Special thanks to:
for their contribution and support.
License
Licensed under MIT