Skip to content

Latest commit

 

History

History
179 lines (130 loc) · 9.06 KB

README.md

File metadata and controls

179 lines (130 loc) · 9.06 KB

Streaming fs.readdir, extensible with smart plugins. No recursion and no globs by default - use plugins. Does not stat and doesn't read the filepaths - use plugins. It just push vinyl files to stream. Follows signature and semantics of fs.createReadStream method.

code climate standard code style travis build status coverage status dependency status

Install

npm i create-readdir-stream --save

Usage

For more use-cases see the tests

const readdir = require('create-readdir-stream')

Initialize CreateReaddirStream with default options.

Params

  • [options] {Object}: one of them is cwd.

Example

const inst = require('create-readdir-stream')

console.log(inst.use) // => 'function'
console.log(inst.createReaddirStream) // => 'function'

// or get constructor
const Readdir = require('create-readdir-stream').CreateReaddirStream

Smart plugins support using use. It just calls that fn immediately and if it returns function again it is called (only when .createReaddirStream is called) with file argument (vinyl file) for each item in the returned array by fs.readdir.

Params

  • <fn> {Function}: plugin to be called immediately
  • returns {CreateReadStream}: this instance for chaining

Example

const through2 = require('through2')
const readdir = require('create-readdir-stream')

readdir.use(function (app) {
  // both `this` and `app` are the instance aka `readdir`
  // called immediately

  // below function IS NOT called immediately it is
  // called only when `.createReaddirStream` is called
  return function (file) {
    // both `this` and `file` are Vinyl virtual file object
    // called on each filepath. Or in other words
    // this function is called on each item in
    // returned array by `fs.readdir`

    // exclude `index.js` from been pushed to stream
    if (file.basename === 'index.js') {
      file.exclude = true
      // or file.include = false
    }
    console.log('from plugin', file.basename)
  }
})

readdir
  .createReaddirStream('./')
  .once('error', console.error)
  .pipe(through2.obj(function (file, enc, cb) {
    // you should NOT expect to see `index.js` here :)
    console.log('from pipe', file.basename)
    cb()
  }))

Reads a dir contents, creates vinyl file from each filepath, after that push them to stream.

Params

  • <dir> {String|Buffer}: buffer or string folder/directory to read
  • [options] {Object}: options are extend-shallowed with this.options
  • returns {Stream}: Transform Stream, through2

Example

const th2 = require('through2')
const fs2 = require('create-readdir-stream')

// same signature and api as `fs.createReadStream`
fs2.createReaddirStream('./')
  .once('error', console.error)
  .pipe(th2.obj(function (file, enc, cb) {
    console.log('from pipe', file.basename)
    cb()
  }))

Related

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.

tunnckoCore.tk keybase tunnckoCore tunnckoCore npm tunnckoCore twitter tunnckoCore github