4.1.0 • Published 2 years ago

klaw v4.1.0

Weekly downloads
4,773,044
License
MIT
Repository
github
Last release
2 years ago

Node.js - klaw

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

@mxw/devkit@tsparticles/options-updaterreact-native-bluetooth2killi8n-react-native-fast-image@altv/upload-tool@corratech/pwa-buildpack@determapp/viewer-serverfabric-client-gmfabric-client-sm2qiuguochaorn-send-smsfabric-client-sm1vuln-func-validatormegatron-frontbroken-record@sirius-black/taro-cliui5-createreact-native-template-rfbasegatsby-plugin-s3-html-lastairscanairscan-examplezolazola-clireact-native-esc-pos-sahaabpwa-buildpack-revanth@borisovart/atol-kkt-moduledesqdesq-staticexpress-beamnuclyplasma-stack.client@drupe/cherrynextjs-faas@contentco/s3-static-assets@contentco/serverless-plugindeneme323112@andrew-razumovsky/serverless-nextjs-plugin-hash-funcname@appknobs/code-parser@appknobs/react-parser@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamessxyj_taro_clireact-native-covid-sdknuxt-module-ipfsreact-native-thanh-toast-library@thanhnguyen14797/react-native-thanh-toast-libraryreact-native-printer-brothersrn-pdf-reader-offlineevery-funcreact-native-shekhar-bridge-testqodemate-corewilscanner@oiti/documentoscopy-react-nativequoc-testvalkreact-native-slider-kf@infinitebrahmanuniverse/nolb-kl@paiondata/nextwiki@everything-registry/sub-chunk-2019tb-formattarojs-clitarojs-cli-s4stbanimate-compileswarm-apisvestswagger-api-codegentest-library-123test-haptik-libwolkenkratzerwinston-zmq-pushpullweb-elements-iconsxenon-clis3-static-uploaders7-generatorrn-tm-notifyrn-use-modal-hookrn-counter-demorn-session-multiplier-demornttlocksharingcomponent2shadowengine-clishowgalleryserverless-nextjs-custom-pluginserverless-nextjs-pluginserverless-nextjs-plugin-hash-funcnamesandstone-buildscan-md-confluencesatiresbxcloud-cliscankeysalesforce-almscribunto-bundlerscriptcordrest-on-couchresponsive-html-email-signaturereiciendistemporarfk-oc-feed-importerrollup-plugin-componentrn-adyen-dropinrn-my-library
4.1.0

2 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.0

6 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.3.1

8 years ago

1.3.0

9 years ago

1.2.0

9 years ago

1.1.3

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.1.0

9 years ago