fstk
v0.2.1
Published
Additions to Node's FS module, a toolkit for filesystem usage
Downloads
95
Readme
fstk
fstk is a Node.js module for extending Node's FS module with various utility methods, without relying on child processes (i.e. bash).
Getting Started
Install the module with: npm install fstk
var fstk = require('fstk');
Methods
All methods accept optional options object and callback as last two arguments (order doesn't matter). If options are not supplied, defaults are used. If callback is not supplied, noop is used.
- tempfile() - Returns a unique temporary file path, using the system's default temp directory
- filename(path) - Returns
path
's base filename with extension removed - replaceExt(path, extension) - Replace
path
's current extension (if any) withextension
- fileType(path) - Returns an intelligent guess of
path
's type based on extension (right now eithervideo
,audio
, orimage
). Mime type list is located in./resources/mimes.json
- subPaths(path) - Returns all intermediate paths (parent directories) to
path
- stat(path, options, callback) - Performs a more comprehensive stat on
path
. Includes automatic evaluation of Node's isFile, isDirectory, ... methods. Includes automatic mime lookup, path normalization, and check for symbolic link. Passoptions.fast_stat
to skip these enhancements. - exists(path, callback) - Checks for
path
's existence and passestrue
orfalse
to callback - transformDir(path, transformer, options, callback) - Reads contents of directory at
path
, performingtransformer
on all file paths found.transformer
is passed a path and a callback to include an error (if it occurs) as first element and and transformed path as second argument. Usesasync.mapSeries
. - statDir(path, callback) - Perform
stat
onpath
and it's top-level contents, passing an error (if applicable) and an object tocallback
containingpath
's stat andpath.files
with an array of stat objects for contents. - dirTree(path, callback) - Recursively read and stat
path
and its subdirectories, passing tocallback
an error (if applicable) and an object representing a tree-like structure ofpath
's deep contents. - dirPart(path, callback) - Partitions the deep contents of
path
into arrays ofdirectories
andfiles
. Passes tocallback
an error (if applicable) and the flattened arrays ofpath
's contents (regardless of specific sub-level). - flattenDir(path, callback) - Flattens the structure of
path
, moving all files to the top level and deleting the now empty subdirectories. Name collisions will cause deeper paths to be overwritten with shallower paths with the same filename. - emptyDir(path, callback) - Deletes files on all levels of
path
, leaving the now empty subdirectories in their original structure. - writeFile(path, body, options, callback) - Identical to Node's
fs.writeFile
but also creates any parent directories that do not exist, similar to bash'smkdir -p
- transformFile(path, transformer, options, callback) - Reads in
path
's data, passes it totransformer
, which passes an error (if it occurs) and the transformed contents ofpath
which are then used to overwritepath
. Passoptions.destination
to write the transformed data somewhere other thanpath
- writeGzipFile(path, body, options, callback) - Compress
data
(gzip) and write the compressed version topath
, includeoptions.encoding
to use encoding other thanutf8
- readGzipFile(path, options, callback) - Read a compressed file at
path
, decompressing it, and passing the resulting buffer tocallback
with an error (if it occured) - writeJSON(path, obj, callback) - Stringify
obj
and write it topath
- readJSON(path, callback) - Parse the contents of
path
and pass results tocallback
as anobject
- updateJSON(path, update, [value], callback) - Update the contents of JSON at
path
, setting propertyupdate
tovalue
, or callingupdate
with JSON set asthis
- mkdir(path, callback) - Similar to bash's
mkdir -p
, makes any missing parent paths forpath
before makingpath
- rmdir(path, callback) - Similar to bash's
rm -rf path
, removes empty and non-empty directories, while also deleting any of their contents - rm(path, callback) - Similar to bash's
rm -rf
, removespath
even if it is a directory (empty or non-empty) - watchFile(path, options, callback) - Poor man's database -- watches a file, loading its updated contents into memory when changes occur and persisting changes to the loaded version to disk. Passing
options.serializer(data, cb)
andoptions.deserializer(data, cb)
provides asynchronous methods used to serialize data (when persisting to disk) and deserialize data (when loading from disk).callback
is passed anobject
tocallback
with the following methods and properties:- watcher - Underlying
FS.fileWatcher
instance - get(callback) - Passes latest file data to
callback
. If a disk read is in progress, callback is blocked until read is completed - set(data, callback) - Sets file data to
data
and persists to disk, callingcallback
when complete - pset(pStr, value, callback) - Sets a deep property (
pStr
) of file data tovalue
and persists all data to disk, callingcallback
when complete
- watcher - Underlying
- watchDirectory(path, options, callback) - watch a directory of files, passing to callback an object including
files
, an object of watched files - watchJSON(path, options, callback) - Uses
watchFile
with JSON serializers and deserializers to keep a JSON file actively updated in memory as anobject
- watchDirectoryJSON(path, options, callback) - watch a directory of JSON files, passing to callback an object including
files
, an object of watched JSON files - getURL(url, options, callback) - Saves contents of
url
to a temporary file. Passoptions.dest_path
to specify a destination path.callback
is passed anerror
andpath
for the newly created file - postPath(path, url, options, callback) - Post the contents of
path
tourl
. Passoptions.method
aspost
orput
to change request type. Passoptions.form_field
to specify a form field to post file contents to. Passoptions.form_data
to include other form data in the request.
License
Copyright (c) 2014 Ben Sack Licensed under the MIT license.