0.2.1 • Published 11 years ago
fstk v0.2.1
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_statto skip these enhancements. - exists(path, callback) - Checks for
path's existence and passestrueorfalseto callback - transformDir(path, transformer, options, callback) - Reads contents of directory at
path, performingtransformeron all file paths found.transformeris 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
statonpathand it's top-level contents, passing an error (if applicable) and an object tocallbackcontainingpath's stat andpath.fileswith an array of stat objects for contents. - dirTree(path, callback) - Recursively read and stat
pathand its subdirectories, passing tocallbackan error (if applicable) and an object representing a tree-like structure ofpath's deep contents. - dirPart(path, callback) - Partitions the deep contents of
pathinto arrays ofdirectoriesandfiles. Passes tocallbackan 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.writeFilebut 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 ofpathwhich are then used to overwritepath. Passoptions.destinationto write the transformed data somewhere other thanpath - writeGzipFile(path, body, options, callback) - Compress
data(gzip) and write the compressed version topath, includeoptions.encodingto use encoding other thanutf8 - readGzipFile(path, options, callback) - Read a compressed file at
path, decompressing it, and passing the resulting buffer tocallbackwith an error (if it occured) - writeJSON(path, obj, callback) - Stringify
objand write it topath - readJSON(path, callback) - Parse the contents of
pathand pass results tocallbackas anobject - updateJSON(path, update, value, callback) - Update the contents of JSON at
path, setting propertyupdatetovalue, or callingupdatewith JSON set asthis - mkdir(path, callback) - Similar to bash's
mkdir -p, makes any missing parent paths forpathbefore 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, removespatheven 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).callbackis passed anobjecttocallbackwith the following methods and properties:- watcher - Underlying
FS.fileWatcherinstance - 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
dataand persists to disk, callingcallbackwhen complete - pset(pStr, value, callback) - Sets a deep property (
pStr) of file data tovalueand persists all data to disk, callingcallbackwhen 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
watchFilewith 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
urlto a temporary file. Passoptions.dest_pathto specify a destination path.callbackis passed anerrorandpathfor the newly created file - postPath(path, url, options, callback) - Post the contents of
pathtourl. Passoptions.methodaspostorputto change request type. Passoptions.form_fieldto specify a form field to post file contents to. Passoptions.form_datato include other form data in the request.
License
Copyright (c) 2014 Ben Sack Licensed under the MIT license.