Fork me on GitHub

fs-tools

Build Status

Useful file utitiles. See API Documentation for detailed info.


walk(path, [pattern,] iterator[, callback])

Recurcively scan files by regex pattern & apply iterator to each. Iterator applied only to files, not to directories. If given path is a file, iterator will be called against it (if pattern allows it).

walkSync(path, [pattern,] iterator)

Sync version of walk(). Throws exception on error.

findSorted(path, [pattern,] callback)

Recursively collects files by regex pattern (if given, all files otherwise).

remove(path, callback)

Recursively delete directory with all content.

removeSync(path)

Sync version of remove(). Throws exception on error.

mkdir(path, mode = '0755', callback)

Recursively make path.

mkdirSync(path, mode = '0755')

Sync version of mkdir(). Throws exception on error.

copy(src, dst, callback)

Copy file.

move(src, dst, callback)

Move file.

tmpdir([template])

Returns unique directory (at the moment of request) pathbname.

License

View the LICENSE file (MIT).

namespace

FsTools

Description

Collection of FS related tools, that stdlib lack of.

class method

FsTools.copy

    • FsTools.copy(src, dst, callback)
      • void
    • src
      • String
    • Source file

    • dst
      • String
    • Destination file

    • callback
      • Function
    • Fired after path has been copied

Copies src to dst, creates directory for given dst with mkdir if needed. Fires callback(err) upon completion.

Example
var src = '/home/nodeca/secrets.yml',
    dst = '/home/nodeca/very/deep/secrets/main.yml';

fstools.copy(src, dst, function (err) {
  if (err) {
    console.log("Failed copy " + src + " into " + dst);
    console.err(err);
    process.exit(1);
  } else {
    console.log("Done!");
    process.exit(0);
  }
});
class method

FsTools.findSorted

    • FsTools.findSorted(pathname, pattern, callback(err, files))
      • Void
    • FsTools.findSorted(pathname, callback(err, files))
      • Void

Finds all files matching pattern withing pathname (including sub-dirs), and passes list of found files (sorted alphabethically) to the callback. If no pattern was given - will collect all found files.

class method

FsTools.mkdir

    • FsTools.mkdir(path, mode, callback)
      • void
    • FsTools.mkdir(path, callback)
      • void
    • path
      • String
    • Path to create

    • mode
      • String
      • Number
    • Permission mode of new directory. See stdlib fs.mkdir for details. Default: '0755'.

    • callback
      • Function
    • Fired after path was created

Creates given path, creating parents recursively if needed. Similar to UNIX' mkdir -pf <path>. After all will fire callback(err) with an error if there were any.

Example
fstools.mkdir('/home/nodeca/media/xxx', function (err) {
  if (err) {
    console.log("Can't' create directory");
    console.err(err);
    process.exit(1);
  } else {
    console.log("We can now store some romantic movies here");
    process.exit(0);
  }
});
class method

FsTools.mkdirSync

    • FsTools.mkdirSync(path[, mode = "0755"])
      • void

Sync version of mkdir.

class method

FsTools.move

    • FsTools.move(source, destination, callback)
      • Void
    • source
      • String
    • Source filename

    • destination
      • String
    • Destination filename

Moves file from source to destination.

class method

FsTools.remove

    • FsTools.remove(path, callback)
      • void
    • path
      • String
    • Path to remove

    • callback
      • Function
    • Fired after path was removed

Removes given path. If it was a directory will remove it recursively, similar to UNIX' rm -rf <path>. After all will fire callback(err) with an error if there were any.

If given path was file - will proxy call to fs.unlink.

Example
fstools.remove('/home/nodeca/trash', function (err) {
  if (err) {
    console.log("U can't touch that");
    console.err(err);
    process.exit(1);
  } else {
    console.log("It's Hammer time");
    process.exit(0);
  }
});
class method

FsTools.removeSync

    • FsTools.removeSync(path)
      • void
    • path
      • String
    • Path to remove

Removes given path. If it was a directory will remove it recursively, similar to UNIX' rm -rf <path>.

If given path was file - will proxy call to fs.unlinkSync.

Example
try {
  fstools.remove('/home/nodeca/trash');
  console.log("It's Hammer time");
  process.exit(0);
} catch (err) {
  console.log("U can't touch that");
  console.err(err);
  process.exit(1);
}
class method

FsTools.tmpdir

    • FsTools.tmpdir([template = "/tmp/fstools.XXXXXX"])
      • String
    • template
      • String
    • Temporary directory pattern.

Returns non-existing (at the moment of request) temporary directory path. template must contain a substring with at least 3 consecutive X, that will be replaced with pseudo-random string of the same length.

Example
fstools.tmpdir('/tmp/fooXXX');      // -> '/tmp/fooa2f'
fstools.tmpdir('/tmp/fooXXX');      // -> '/tmp/foocb1'
fstools.tmpdir('/tmp/foo-XXXXXX');  // -> '/tmp/foo-ad25e0'
class method

FsTools.walk

    • FsTools.walk(path, pattern, iterator, callback)
      • void
    • FsTools.walk(path, iterator, callback)
      • void

Walks throught all files withing path (including sub-dirs) and calls iterator on each found file (or block device etc.) matching pattern. If no pattern was given - will fire call iterator for every single path found. After all iterations will call callback (if it was specified) with passing error as first arguemtn if there was an error.

If path points a file, iterator will be called against it (respecting pattern if it was given).

Iterator

All iterations are running within promise. So callback given to the walk will fire only after all iterator callbacks willnotify they finished their work:

var iterator = function (path, stats, callback) {
  // ... do something
  if (err) {
    // ... if error occured we can "stop" walker
    callback(err);
    return;
  }
  // ... if everything is good and finished notify walker we're done
  callback();
};

Iterator is called with following arguments:

  • path (String): Full path of the found element (e.g. /foo/bar.txt)
  • stats (fs.Stats): Stats object of found path
  • callback (Function): Callback function to call after path processing
Example
fstools.walk('/home/nodeca', function (path, stats, callback) {
  if (stats.isBlockDevice()) {
    callback(Error("WTF? Block devices are not expetcted in my room"));
    return;
  }

  if (stats.isSocket()) {
    console.log("Finally I found my socket");
  }

  callback();
}, function (err) {
  if (err) {
    // shit happens!
    console.error(err);
    process.exit(1);
  }

  console.log("Hooray! We're done!");
});
Example (using pattern matching)
fstools.walk('/home/nodeca', '\.yml$', function (path, stats, callback) {
  fs.readFile(path, 'utf-8', funtion (err, str) {
    if (err) {
      callback(err);
      return;
    }

    console.log(str);
    callback();
  });
}, function (err) {
  if (err) {
    console.error(err);
  }

  console.log('Done!');
});
class method

FsTools.walkSync

    • FsTools.walkSync(path, pattern, iterator)
      • void
    • FsTools.walkSync(path, iterator)
      • void

Synchronous version of walk.