sander
A Promise-based power tool for common filesystem tasks in node.js.
Installation
npm install sander
fs
? Really?
Another wrapper around Yup. Working with the low-level fs
API is the fastest road to callback hell, and while a lot of the existing fs
wrappers add a whole load of missing features, they don't really mitigate the fundamental suckiness of working with the filesystem in a painful, imperative way, which forces you to handle errors at every step of the journey towards the centre of the node.js pyramid of doom.
Enough! Manual filing is tedious - you need a power tool. Instead of writing this...
var path = fs = mkdirp = ; var dest = path; ;
...write this:
var sander = ;sander;
It uses graceful-fs rather than the built-in fs
module, to eliminate EMFILE from the list of things you have to worry about.
Conventions
Promises
All async methods (those whose fs
equivalents would take a callback, e.g. sander.readFile
) return a Promise. If you're not familiar with Promises, read up on them on MDN - they're coming in ES6 and are already supported in many browsers, and I guarantee they'll make your life easier.
(Node doesn't natively support promises yet - we're using es6-promise for maximum compatibility. For convenience, the Promise
constructor is exposed as sander.Promise
.)
Intermediate folder creation
When writing files and folders, intermediate folders are automatically created as necessary. (I've never encountered a situation where I wanted an ENOENT
error instead of having this be done for me.)
Automatic path resolution
Wherever appropriate, method arguments are joined together with path.resolve()
- so the following are equivalent:
sander;sander;sander; // or 'foo\bar\baz' on Windows
Methods that involve two paths
Some operations, such as renaming files, require two paths to be specified. The convention for handling this in sander is as follows:
sander;
Methods
fs
methods
In addition to the extra methods (listed below), all fs
methods have sander
equivalents. The synchronous methods (those ending Sync
) are the same as the fs
originals except that path resolution and intermediate folder creation are automatically handled (see conventions, above). All async methods return a promise.
For more information about what these methods do, consult the node documentation.
In the list below, ...paths
indicates you can use one or more strings in sequence, as per the automatic path resolution convention. An fd
argument refers to a file descriptor, which you'd generate with sander.open()
or sander.openSync()
. Arguments wrapped in []
characters are optional.
sandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersanderlink...pathssandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersandersander
Note that with the chmod
/fchmod
/lchmod
/symlink
/mkdir
/open
methods (and their synchronous equivalents), the mode
and type
arguments must be passed as objects with a mode
or type
property. This is so that sander knows which arguments should be treated as parts of a path (because they're strings) and which shouldn't.
The same is true for methods like readFile
- whereas in node you can do fs.readFile('path/to/file.txt', 'utf-8')
if you want to specify utf-8 encoding, with sander the final argument should be a {encoding: 'utf-8'}
object.
Extra methods
// Copy a file using streams. `readOptions` is passed to `fs.createReadStream`,// while `writeOptions` is passed to `fs.createWriteStream`sander // Copy a file synchronously. `readOptions`, is passed to `fs.readFileSync`,// while `writeOptions` is passed to `fs.writeFileSync`sander // Copy a directory, recursively. `readOptions` and `writeOptions` are// treated as per `sander.copyFile[Sync]`sandersander // List contents of a directory, recursivelysandersander // Remove a directory and its contentssandersander // Symlink a file or directory, unless we're on Windows in which// case fall back to copying to avoid permissions issuessander;sander;
License
MIT