Home

Awesome

fs2

Functions that complement and extend fs package

Originally derived from node-ext package.

Installation

$ npm install fs2

API

access(path[, mode[, cb]]) (fs2/access)

Same as fs.access. Returns promise.

chmod(path, mode[, cb]) (fs2/chmod)

Same as fs.chmod. Returns promise.

Not available on Windows.

Extra options:

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

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

Extra options:

copyDir(src, dest[, options[, cb]]) (fs2/copy-dir)

Copy directory recursively, returns promise but accepts as well regular callback.

Supported options:

descriptorsHandler() (fs2/descriptors-handler)

Initializes EMFILE errors prevention.

To be used only in main modules. Never require it in generic module that may be required in others

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

emptyDirSync(path[, options]) (fs2/empty-dir-sync)

Empty directory synchronously

Supported options:

hasAccess(path[, options[, cb]]) (fs2/has-access)

Conditional version of access. Returns promise.

Supported options:

isDirectory(path[, cb]) (fs2/is-directory)

Whether path points to a directory Resolves with true if provided path points to a directory, false if provided path points to a non-directory file, null if there's no file at path. Returns promise.

isFile(path[, cb]) (fs2/is-file)

Whether path points to existing file Resolves with true if provided path points to a file, false if provided path points to existing non-directory file, null if there's no file at path. Returns promise.

isIgnored(mode, path[, options[, cb]]) (fs2/is-ignored)

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:

isSymlink(path[, options[, cb]]) (fs2/is-symlink)

Whether path points to a symlimk Resolves with true if provided path points to symlink, false if provided path points to a non-symlink file, null if there's no file at path. Returns promise.

Additionally resultion can be fine tuned for specific needs with below options:

Supported options:

lchmod(path, mode[, cb]) (fs2/lchmod)

Same as fs.lchmod. Returns promise.

Only available on Mac OS X.

lstat(path[, cb]) (fs2/lstat)

Same as fs.lstat. Returns promise.

Supported options:

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

Extended version of native mkdir. Returns promise

Supported options:

readFile(path[, options][, cb]) (fs2/read-file)

Extended version of native fs.readFile. Returns promise

Supported options:

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

Extended version of native fs.readdir. Returns promise

Suported options:

readlink(path[, options[, cb]]) (fs2/readlink)

Same as fs.readlink. Returns promise. Supports loose: true option, which when passed, resolves with null (instead of exception) if file at path is not a symlink or it doesn't exist.

realpath(path[, options[, cb]]) (fs2/realpath)

Same as fs.realpath. Returns promise. Supports loose: true option, which when passed, resolves with null (instead of exception) if path doesn't exist

rename(oldPath, newPath[, cb]) (fs2/rename)

Same as fs.rename. Returns promise.

Supported options:

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

Extended version of native rmdir. Returns promise

Supported options:

rmdirSync(path[, options]) (fs2/rmdir-sync)

Extended version of native rmdirSynnc.

Supported options:

stat(path[, cb]) (fs2/stat)

Same as fs.stat. Returns promise.

Supported options:

symlink(srcPath, dstPath[, type[, cb]]) (fs2/symlink)

Same as fs.symlink. Returns promise that resolves with true if symlink was created (false can be returned with loose option, when symlink already exists)

Supported options:

typeByStats(stats) (fs2/type-by-stats)

Returns type of file according to provided stats object.

unlink(path[, cb]) (fs2/unlink)

Same as fs.unlink. Returns promise.

Supported options:

unlinkSync(path) (fs2/unlink-sync)

Same as fs.unlinkSync, but with support for extra options.

Supported options:

watchPath(path) (fs2/watch-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) (fs2/watch)

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[, options|encoding[, callback]]) (fs2/write-file)

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).

Supported options:

Tests Build Status

$ npm test