fs2

fs (file system package) extensions

npm install fs2
15 downloads in the last day
118 downloads in the last week
582 downloads in the last month

fs2 - Node.js fs package extensions

Functions that extend and complement Node.js fs package

Originally derived from node-ext package.

Installation

$ npm install fs2

API

chmod(path, mode[, cb])

Same as fs.chmod. Returns promise.

Not available on Windows.

copy(src, dest[, options, cb])

Copy file, returns promise but accepts as well regular callback. Eventual options are passed to underlying fs.createWriteStream

descriptorsHandler()

Initializes EMFILE errors prevention.
How it works? If limit of descriptors is reached it holds the calls to native functions and releases them when taken descriptors are freed.

Internally it provides same solution as fs-graceful module with following differences:

  1. Focuses only on file descriptors limit problem
  2. Gives access to taken/available descriptors count and allows setting of limit by external module. Thanks to that we can also cover descriptors opened by fs.watch module (watch is bound to that module)
  3. Covers readdir calls (which also happen to throw EMFILE errors)
  4. More bulletproof (I assume) error handling logic

isIgnored(mode, path[, options][, cb])

Whether file is ignored up to predefined rules. Returns promise but regular callback is also supported.

Rules are decided by mode argument. Currently only git mode is supported, in that case rules are searched in .gitignore files (Rules have effect only if placed in valid git repositories). Other modes can be easily configured by extending _ignoreModes module (See lib/fs/_ignore-modes directory to see how it's done).

Supported options:

  • globalRules string|array - additional global rules. They will be matched as if placed in filesystem root directory, it means that any rules found in existing ignore files may override them.
  • watch bool - whether to watch for changes. If ignore state would change, returned promise would emit change event with new value (true/false)

lchmod(path, mode[, cb])

Same as fs.lchmod. Returns promise.

Only available on Mac OS X.

lstat(path[, cb])

Same as fs.lstat. Returns promise.

mkdir(path[, options|mode][, cb])

Extended version of native mkdir. Returns promise

Supported options:

  • mode - Reflects mode in native version
  • intermediate - Whether to create directories recursively (if parent is not created), reflects mkir -p, internal implementation inspired by Substack's node-mkdirp

readFile(path[, options][, cb])

Extended version of native fs.readFile. Returns promise

Supported options:

  • loose - Do not error if file doesn't exits or is inaccessible, return null instead.
  • watch - Whether to watch file for changes. Changes are emited via change event on returned promise. If file was removed and loose option is off, end event is emitted and watcher is closed

readdir(path[, options][, cb])

Extended version of native fs.readdir. Returns promise

Suported options:

  • depth number- Level of recurse into subdirectories. Defaults to 0 which resembles behavior of native version. If you want to recurse without any nest limitation just provide Infinity
  • type object- Which type of files should be returned. By default all files are returned. Stats methods shows how many different types can be returned. To narrow it down provide a hash. e.g. { file: true, symbolicLink: true }.
  • pattern regexp- Filter returned files by specific pattern. Pattern should be regular expression that would be matched against full path.
  • watch bool - Watch directory for changes. Changes are emitted with data events, listeners are called with event object which states which files were added (event.added) and which were removed (event.removed), Starting from next release (v0.4) this functionality will be provided as valid Node.js stream
  • stream bool - Whether to provide data continuously. Currently it's not provided as stream per se (it would be starting from next release, v0.4). Currently data is emited as data events with event objects described under watch option
  • ignoreRules string|array - Whether to obey ignore rules found in ignore files. See fs.isIgnored for more information
  • globalRules string|array - Global rules that complement ignoreRules. See fs.isIgnored for more information.

rmdir(path[, options][, cb])

Extended version of native rmdir. Returns promise

Supported options:

  • recursive - Attempt to remove directory with subdirectories recursively.
  • force - Attempt to remove other files within directory as well.

typeByStats(stats)

Returns type of file according to provided stats object.

Same as fs.unlink. Returns promise.

watchPath(path)

Watch specific path for changes. It's about observing specific file path (not directory content). change events are emitted with event object where event.type says wether file was created, modified or removed.

watch(path)

Watch file for changes.
fs.watch wrapper that works same way on every platform, always configured in persistent: false mode. It's aware of open file descriptors limitations, if EMFILE error is approach, switch to alternative mode that pings file stats (see fs.watchFile) is made.

writeFile(filename, data[, encoding][, callback])

Same as native fs.writeFile but safe for simultaneous calls of write to same file (in such case current write will be abandonded, and new would be started).

Tests Build Status

$ npm test
npm loves you