Package Exports
- klaw
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) 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
A Node.js file system walker extracted from fs-extra.
Install
npm i --save klawName
klaw is walk backwards :p
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. Typestring.options: Readable stream options and the following:queueMethod: Either'shift'or'pop'. Typestring.pathSorter: Sorting function for Arrays. Typefunction.
Streams 1 (push) example:
var klaw = require('klaw')
var items = [] // files, directories, symlinks, etc
klaw('/some/dir')
.on('data', function (item) {
items.push(item.path)
})
.on('end', function () {
console.dir(items) // => [ ... array of files]
})Streams 2 & 3 (pull) example:
var klaw = require('klaw')
var items = [] // files, directories, symlinks, etc
klaw('/some/dir')
.on('readable', function () {
var item
while ((item = this.read())) {
items.push(item.path)
}
})
.on('end', function () {
console.dir(items) // => [ ... array of files]
})If you're not sure of the differences on Node.js streams 1, 2, 3 then I'd recommend this resource as a good starting point: https://strongloop.com/strongblog/whats-new-io-js-beta-streams3/.
Error Handling
Listen for the error event.
Example:
var klaw = require('klaw')
klaw('/some/dir')
.on('readable', function () {
var item
while ((item = this.read())) {
// do something with the file
}
})
.on('error', function (err, item) {
console.log(err.message)
console.log(item.path) // the file the error occurred on
})
.on('end', function () {
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 through2Example (skipping directories):
var klaw = require('klaw')
var through2 = require('through2')
var excludeDirFilter = through2.obj(function (item, enc, next) {
if (!item.stats.isDirectory()) this.push(item)
next()
})
var items = [] // files, directories, symlinks, etc
klaw('/some/dir')
.pipe(excludeDirFilter)
.on('data', function (item) {
items.push(item.path)
})
.on('end', function () {
console.dir(items) // => [ ... array of files without directories]
})
Example (totaling size of PNG files):
var klaw = require('klaw')
var path = require('path')
var through2 = require('through2')
var totalPngsInBytes = 0
var 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', function (item) {
items.push(item.path)
})
.on('end', function () {
console.dir(totalPngsInBytes) // => total of all pngs (bytes)
})Example (deleting all .tmp files):
var fs = require('fs')
var klaw = require('klaw')
var through2 = require('through2')
var deleteAction = through2.obj(function (item, enc, next) {
this.push(item)
if (path.extname(item.path) === 'tmp') {
item.deleted = true
fs.unklink(item.path, next)
} else {
item.deleted = false
next()
}
})
var deletedFiles = []
klaw('/some/dir')
.pipe(deleteAction)
.on('data', function (item) {
if (!item.deleted) return
deletedFiles.push(item.path)
})
.on('end', function () {
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', function () {
console.log('all done!')
})Searching Strategy
Pass in options for queueMethod and pathSorter 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