create-readdir-stream
v1.0.0
Published
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.creat
Downloads
25
Readme
create-readdir-stream
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 offs.createReadStream
method.
Table of Contents
Install
npm i create-readdir-stream --save
Usage
For more use-cases see the tests
const readdir = require('create-readdir-stream')
API
CreateReaddirStream
Initialize
CreateReaddirStream
with defaultoptions
.
Params
[options]
{Object}: one of them iscwd
.
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
.use
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) withfile
argument (vinyl file) for each item in the returned array byfs.readdir
.
Params
<fn>
{Function}: plugin to be called immediatelyreturns
{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()
}))
.createReaddirStream
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 withthis.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
- bash-glob: Bash-powered globbing for node.js | homepage
- glob: a little globber | homepage
- ip-filter: Validates valid IPs (IPv4 and IPv6) using micromatch - glob patterns, RegExp… more | homepage
- is-match-ip: Matching IPs using micromatch and ip-filter - glob patterns, RegExp, string or… more | homepage
- koa-better-router: Fast, simple, smart and correct routing for koa, using path-match. Foundation for… more | homepage
- micromatch: Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch… more | homepage
- nanomatch: Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch… more | homepage
- vinyl: Virtual file format. | homepage
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.