JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3971948
  • Score
    100M100P100Q204439F
  • License MIT

Recursive and synchronous file system walker

Package Exports

  • klaw-sync

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (klaw-sync) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Node.js: klaw-sync

npm Package Build Status windows Build status

Standard JavaScript

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')
// paths = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/file1', stats: {}}]

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})
// files = [{path: '/some/dir/file1', stats: {}}, {path: '/some/dir/file2', stats: {}}]

directories only

var klawSync = require('klaw-sync')
var dirs = klawSync('/some/dir', {nofile: true})
// dirs = [{path: '/some/dir/dir1', stats: {}}, {path: '/some/dir/dir2', stats: {}}]

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 using 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