A collection of recursive filesystem utilities.
Installation
npm install recur-fs
Usage
var fs = ; // Gather all nested files and directoriesfs;
API
readdir(directory, visitor(resource, stat, next), fn(err, resources)) Recursively read contents of directory
, returning all resources. visitor
is an optional function called on each resource. Calling next(false)
from visitor
will exclude resource from the collection.
fs; fs;
readdir.sync(directory, visitor(resource, stat)) Synchronously, recursively read contents of directory
, returning all resources. visitor
is an optional function called on each resource. Returning false
from visitor
will exclude resource from the collection.
var resources = fsreaddir; var files = fsreaddir;
walk(directory, visitor(resource, stat, next), fn(err)) Walk up filesystem tree from directory
, passing all resources to visitor
, and stopping when root directory is reached. Calling next(true)
will abort walking before completion.
fs;
walk.sync(directory, visitor(resource, stat)) Synchronously walk up filesystem tree from directory
, passing all resources to visitor
, and stopping when root directory is reached, or visitor returns true
.
fswalk;
hunt(directory, matcher(resource, stat, next), stopOnFirstMatch, fn(err, matches)) Walk up filesystem tree from directory
, returning all resources matched with matcher
, and stopping when root directory is reached, or after first match if stopOnFirstMatch=true
.
matcher
can be a glob-type string (see minimatch), or function calling next(true)
to signal a match. In addition, next
also accepts a second argument in order to abort before completion.
fs; fs; fs;
hunt.sync(directory, matcher(resource, stat), stopOnFirstMatch) Synchronously walk up filesystem tree from directory
, returning all resources matched with matcher
, and stopping when root directory is reached, or after first match if stopOnFirstMatch=true
.
matcher
can be a glob-type string (see minimatch), or function returning true
to signal a match.
var jsFiles = fshunt; var cssFile = fshunt; var index = fshunt;
cp(source, destination, force, fn(err, filepath)) Recursively copy source
to destination
(cp -r
). Copies contents of source
directory if path contains a trailing /
. force=true
will overwrite destination
if it already exists.
fs; // Copy directory contents (note trailing slash)fs;
cp.sync(source, destination, force) Synchronously, recursively copy source
to destination
(cp -r
). Copies contents of source
directory if path contains a trailing /
. force=true
will overwrite destination
if it already exists.
var filepath = fs;
mkdir(directory, fn(err)) Recursively create nested directory
(mkdir -p
). If directory
looks like a filepath (has .extension), directories will be created at path.dirname(directory)
.
fs;
mkdir.sync(directory) Synchronously, recursively create nested directory
(mkdir -p
). If directory
looks like a filepath (has .extension), directories will be created at path.dirname(directory)
.
fsmkdir;
mv(source, destination, force, fn(err, filepath)) Move source
to destination
, including all contents of source
if directory. force=true
will overwrite destination
if it already exists.
fs;
mv.sync(source, destination, force) Synchronously move source
to destination
, including all contents of source
if directory. force=true
will overwrite destination
if it already exists.
fsmv;
rm(source, fn(err)) Recursively remove source
(rm -rf
). Prevents removal of resources outside of process.cwd()
.
fs;
rm.sync(source) Synchronously, recursively remove source
(rm -rf
). Prevents removal of resources outside of process.cwd()
.
fsrm;
indir(directory, filepath) Check that filepath
is likely child of directory
. NOTE: only makes string comparison.
fs;