• Stars
    star
    316
  • Rank 132,587 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Node.js file system walker with a Readable stream interface. Extracted from fs-extra.

Node.js - klaw

JavaScript Standard Style

A Node.js file system walker extracted from fs-extra.

npm Package build status windows build status

Install

npm i --save klaw

If you're using Typescript, we've got types:

npm i --save-dev @types/klaw

Name

klaw is walk backwards :p

Sync

If you need the same functionality but synchronous, you can use klaw-sync.

Usage

klaw(directory, [options])

Returns a Readable stream that iterates through every file and directory starting with dir as the root. Every read() or data event returns an object with two properties: path and stats. path is the full path of the file and stats is an instance of fs.Stats.

  • directory: The directory to recursively walk. Type string or file URL.
  • options: Readable stream options and the following:
    • queueMethod (string, default: 'shift'): Either 'shift' or 'pop'. On readdir() array, call either shift() or pop().
    • pathSorter (function, default: undefined): Sorting function for Arrays.
    • fs (object, default: graceful-fs): Use this to hook into the fs methods or to use mock-fs
    • filter (function, default: undefined): Filtering function for Arrays
    • depthLimit (number, default: undefined): The number of times to recurse before stopping. -1 for unlimited.
    • preserveSymlinks (boolean, default: false): Whether symlinks should be followed or treated as items themselves. If true, symlinks will be returned as items in their own right. If false, the linked item will be returned and potentially recursed into, in its stead.

Streams 1 (push) example:

const klaw = require('klaw')

const items = [] // files, directories, symlinks, etc
klaw('/some/dir')
  .on('data', item => items.push(item.path))
  .on('end', () => console.dir(items)) // => [ ... array of files]

Streams 2 & 3 (pull) example:

const klaw = require('klaw')

const items = [] // files, directories, symlinks, etc
klaw('/some/dir')
  .on('readable', function () {
    let item
    while ((item = this.read())) {
      items.push(item.path)
    }
  })
  .on('end', () => console.dir(items)) // => [ ... array of files]

for-await-of example:

for await (const file of klaw('/some/dir')) {
  console.log(file)
}

Error Handling

Listen for the error event.

Example:

const klaw = require('klaw')

klaw('/some/dir')
  .on('readable', function () {
    let item
    while ((item = this.read())) {
      // do something with the file
    }
  })
  .on('error', (err, item) => {
    console.log(err.message)
    console.log(item.path) // the file the error occurred on
  })
  .on('end', () => console.dir(items)) // => [ ... array of files]

Aggregation / Filtering / Executing Actions (Through Streams)

On many occasions you may want to filter files based upon size, extension, etc. Or you may want to aggregate stats on certain file types. Or maybe you want to perform an action on certain file types.

You should use the module through2 to easily accomplish this.

Install through2:

npm i --save through2

Example (skipping directories):

const klaw = require('klaw')
const through2 = require('through2')

const excludeDirFilter = through2.obj(function (item, enc, next) {
  if (!item.stats.isDirectory()) this.push(item)
  next()
})

const items = [] // files, directories, symlinks, etc
klaw('/some/dir')
  .pipe(excludeDirFilter)
  .on('data', item => items.push(item.path))
  .on('end', () => console.dir(items)) // => [ ... array of files without directories]

Example (ignore hidden directories):

const klaw = require('klaw')
const path = require('path')

const filterFunc = item => {
  const basename = path.basename(item)
  return basename === '.' || basename[0] !== '.'
}

klaw('/some/dir', { filter: filterFunc })
  .on('data', item => {
    // only items of none hidden folders will reach here
  })

Example (totaling size of PNG files):

const klaw = require('klaw')
const path = require('path')
const through2 = require('through2')

let totalPngsInBytes = 0
const aggregatePngSize = through2.obj(function (item, enc, next) {
  if (path.extname(item.path) === '.png') {
    totalPngsInBytes += item.stats.size
  }
  this.push(item)
  next()
})

klaw('/some/dir')
  .pipe(aggregatePngSize)
  .on('data', item => items.push(item.path))
  .on('end', () => console.dir(totalPngsInBytes)) // => total of all pngs (bytes)

Example (deleting all .tmp files):

const fs = require('fs')
const klaw = require('klaw')
const through2 = require('through2')

const deleteAction = through2.obj(function (item, enc, next) {
  this.push(item)

  if (path.extname(item.path) === '.tmp') {
    item.deleted = true
    fs.unlink(item.path, next)
  } else {
    item.deleted = false
    next()
  }
})

const deletedFiles = []
klaw('/some/dir')
  .pipe(deleteAction)
  .on('data', item => {
    if (!item.deleted) return
    deletedFiles.push(item.path)
  })
  .on('end', () => console.dir(deletedFiles)) // => all deleted files

You can even chain a bunch of these filters and aggregators together. By using multiple pipes.

Example (using multiple filters / aggregators):

klaw('/some/dir')
  .pipe(filterCertainFiles)
  .pipe(deleteSomeOtherFiles)
  .on('end', () => console.log('all done!'))

Example passing (piping) through errors:

Node.js does not pipe() errors. This means that the error on one stream, like klaw will not pipe through to the next. If you want to do this, do the following:

const klaw = require('klaw')
const through2 = require('through2')

const excludeDirFilter = through2.obj(function (item, enc, next) {
  if (!item.stats.isDirectory()) this.push(item)
  next()
})

const items = [] // files, directories, symlinks, etc
klaw('/some/dir')
  .on('error', err => excludeDirFilter.emit('error', err)) // forward the error on
  .pipe(excludeDirFilter)
  .on('data', item => items.push(item.path))
  .on('end', () => console.dir(items)) // => [ ... array of files without directories]

Searching Strategy

Pass in options for queueMethod, pathSorter, and depthLimit to affect how the file system is recursively iterated. See the code for more details, it's less than 50 lines :)

License

MIT

Copyright (c) 2015 JP Richardson

More Repositories

1

node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
JavaScript
9,368
star
2

string.js

Extra JavaScript string methods.
JavaScript
1,803
star
3

node-jsonfile

Easily read/write JSON files.
JavaScript
1,203
star
4

node-google

A Node.js module to search and scrape Google.
JavaScript
454
star
5

sublime-js-snippets

Snippets for JavaScript / JS Programming in Sublime Text 2 & 3
407
star
6

electron-mocha

Run Mocha tests in Electron
JavaScript
345
star
7

electron-window

Convenience methods for Electron windows.
JavaScript
290
star
8

node-suppose

Like UNIX Expect, but for Node.js.
JavaScript
252
star
9

node-death

Gracefully cleanup when termination signals are sent to your process.
JavaScript
182
star
10

electron-ipc-stream

Duplex stream that runs over Electron's IPC
JavaScript
152
star
11

asciiflow

ASCII diagrams and drawing on the web (GWT)
Java
135
star
12

tin

Easily manage package.json, component.json, and bower.json files.
JavaScript
92
star
13

is-electron-renderer

Check if code is running in Electron renderer process
JavaScript
78
star
14

node-github-download

Easily download Github repos without dependencies such as Git, Tar, Unzip, etc.
JavaScript
76
star
15

secure-random

A simple JavaScript component to normalize the creation of cryptographically strong random values.
JavaScript
62
star
16

iceden

Maybe something like Firebase?
JavaScript
60
star
17

GeneratePushCerts

Automation of Generating Push Certificates for iOS Devices
Ruby
57
star
18

node-batchflow

Batch process collections in parallel or sequentially.
JavaScript
51
star
19

node-kexec

Node.js exec function to replace running process; like Ruby's exec.
C++
50
star
20

tape-promise

Promise and async/await support for Tape
JavaScript
46
star
21

talks-ncc3-demo

Demo from Nebraska Code Camp #3
JavaScript
40
star
22

node-nextflow

A simple control-flow library for Node.js targetted towards CoffeeScript developers.
CoffeeScript
38
star
23

node-scrap

A simple screen scraper module that uses jQuery style semantics.
JavaScript
33
star
24

react-qr

A React.js QR Code component.
JavaScript
32
star
25

ansible-redis

Ansible playbook to install redis.
Shell
29
star
26

node-canada

Cities and provinces from Canada for JavaScript
JavaScript
29
star
27

cross-zip-cli

Zip/Unzip directories cross platform from the CLI. Great for npm scripts.
JavaScript
26
star
28

node-path-extra

Node.js: extra methods for the path object.
JavaScript
23
star
29

readline-go

golang: Easily read lines from a stream such as `stdin` for a file. Supports either `\n`, `\r\n`, or mixed.
Go
22
star
30

least-squares

JavaScript component for linear least squares regression analysis.
JavaScript
19
star
31

CommonLib

A .NET Library With Utility Classes and Methods
C#
19
star
32

node-linkscrape

A Node.js module to scrape and normalize links from an HTML string.
JavaScript
15
star
33

FridayThe13th

Fast JSON Parser for Silverlight or .NET
C#
14
star
34

ip-location

Get an IP or hostname location geo coordinates.
JavaScript
14
star
35

jsock

JavaScript component for easy JSON handling over sockets or streams. Works in Node.js or the browser.
JavaScript
13
star
36

rr

A simple JavaScript component to iterate an array round robin.
JavaScript
12
star
37

node-packpath

Easily find the path(s) of package.json.
JavaScript
12
star
38

node-markdown-extra

Extra markdown methods.
JavaScript
12
star
39

buffer-json

JSON reviver/replacer methods for JavaScript Buffer type.
JavaScript
12
star
40

npm-latest

Quickly find the latest version of a package in npm.
JavaScript
12
star
41

keychain_manager

Ruby Gem for OS X Keychain Access
Ruby
11
star
42

npm-research

Nice little utility to help you research NPM packages.
JavaScript
10
star
43

bitcoin-faucet

A Node.js app to easily create a programmable Bitcoin Testnet faucet. This allows you to easily test your Bitcoin applications.
JavaScript
10
star
44

logmeup

LogMeUp Server - View any log files real-time in your web browser.
JavaScript
10
star
45

fix-dropbox-node_modules-symlinks

Fix Dropbox `node_modules/.bin/` symlinks.
JavaScript
10
star
46

mars

Forget live coding, easy code demos.
JavaScript
9
star
47

angular-bluebird

An AngularJS service for Bluebird promise library.
JavaScript
8
star
48

atom-rename-tabs

Rename tabs titles with previous directory. Consistent with Sublime Text Editor behavior.
JavaScript
8
star
49

node-cfs

Node.js conditional file streams
JavaScript
7
star
50

d3-measure-text

A JavaScript component to measure the the width and height of SVG text.
JavaScript
7
star
51

d3-dragrect

A JavaScript D3 drag selection rectangle component.
JavaScript
6
star
52

secret-box

Encrypt and decrypt secrets. Built on AES-256-GCM and Scrypt for now.
JavaScript
6
star
53

MemMapCache

A .NET caching solution that uses memory mapping.
C#
5
star
54

ospath

A JavaScript component that provides operating specific path values.
JavaScript
5
star
55

jsontocsv

Convert lines of JSON data to CSV
JavaScript
5
star
56

d3-tooltip

This is a JavaScript d3 tooltip component.
JavaScript
5
star
57

mongo_install

Ruby script to install download, install, and setup MongoDB as a service on a Ubuntu or Debian box.
Ruby
5
star
58

potter-wordpress

Command line tool to export WordPress to static Markdown.
JavaScript
5
star
59

node-batchtransform

Batch transform/convert a collection of files e.g. convert a collection of markdown template files to html files.
JavaScript
5
star
60

is-async-fn

Check if something is an ES7 async function.
JavaScript
4
star
61

cb-insight

Common Blockchain wrapper for Bitpay Insight API
JavaScript
4
star
62

node-dh

Distributed hash. Simple Node.js wrapper for Redis hash.
JavaScript
4
star
63

spend

A JavaScript component to create simple Bitcoin / Testnet transactions for integration testing with the actual network.
JavaScript
4
star
64

ripdb

100% JavaScript embeddable JSON time series database.
JavaScript
4
star
65

node-worddump

Dump or Export your WordPress blog content.
JavaScript
3
star
66

procbits.com

Home of my current coding blog.
HTML
3
star
67

react-password

A React.js component for password inputs.
JavaScript
3
star
68

ansible-rethinkdb

Ansible playbook to setup a RethinkDB.
3
star
69

node-readline-prompter

Easily prompt the user with a series of questions.
JavaScript
3
star
70

issue-links

Help to keep tidy `CHANGELOG.md`/`HISTORY.md` files by keeping your GitHub Issue links up to date.
JavaScript
3
star
71

atom-javascript-standard-snippets

JavaScript Standard Style Snippets for Atom
CoffeeScript
3
star
72

pottercms

https://github.com/skywrite/sky
JavaScript
3
star
73

d3-chart

A simple JavaScript D3.js chart.
3
star
74

create-output-stream

Node.js: exactly like `fs.createWriteStream`, but if the directory does not exist, it's created.
JavaScript
3
star
75

node-wp2md

Convert your WordPress blog to Markdown.
JavaScript
3
star
76

node-parentpath

Find a path in a parent directory.
JavaScript
3
star
77

buzz

Keep an app running indefinitely, kill it occasionally if you want.
JavaScript
3
star
78

cooking-recipes

I'm starting to write down recipes that I've tried.
3
star
79

node-tweezers

Extract mustache tokens from a file or string.
JavaScript
2
star
80

node-logmeup

Node.js plugin to interface to LogMeUp
CoffeeScript
2
star
81

node-testutil

Node.js testing utilities
JavaScript
2
star
82

node-autoresolve

A simple Node.js module to auto resolve package paths.
JavaScript
2
star
83

ip-location-cli

Command line utility to fetch IP geo location or your own location.
JavaScript
2
star
84

electron-menu

Convenience module to build Electron menu templates.
JavaScript
2
star
85

node-batchfile

JavaScript
2
star
86

node-vcsurl

Convert VCS repository URLs like Github or Bitbucket to their http equivalents.
JavaScript
2
star
87

node-markdown-page

Methods for parsing a markdown page for a blog post or documentation.
JavaScript
2
star
88

node-dq

Simple Node.js priority queue built on Redis.
JavaScript
2
star
89

ymd

JavaScript component to return the year, month, and day string.
JavaScript
2
star
90

terst

A JavaScript testing component with a terse syntax. Supported in both Node.js and the browser.
JavaScript
2
star
91

browser-storage

Normalizes local-storage behavior between node and browser
JavaScript
2
star
92

is-os-cli

CLI utility to check if operating system. Useful for npm scripts.
JavaScript
2
star
93

prevent-backspace

Prevents the backspace from navigating back in the browser.
JavaScript
2
star
94

goatee-go

Non-verbose testing in Go
Go
1
star
95

node-markdown-walker

Simple directory walker that specifically looks for markdown files.
JavaScript
1
star
96

node-cl

Easily create command line programs and interfaces in Node.js.
JavaScript
1
star
97

node-proxyinfo

Get information about HTTP proxy servers
CoffeeScript
1
star
98

trinity

JavaScript
1
star
99

node-qflow

A very simple data queue processing library.
JavaScript
1
star
100

ansible-phantomjs

Ansible playbook to install PhantomJS on Ubuntu server(s).
1
star