profs
v1.1.1
Published
A promissory fs module with extra helpful goodness
Downloads
3
Readme
Profs exports the primisified fs library and includes multiple customized implimentation from fs-extra such as walk, touch, and mkdirp.
mkdirp
Acts like the linux command "mkdir -p" and creates the full path to a directory.
mkdirp
Will not throw EEXIST
or ENOENT
but will throw for any other error.
const fs = require('profs')
var path = 'this/doesnt/exit/yet'
fs.mkdirp(path).then(() => console.log('done!'))
touch & touchp
Acts like the linux command "touch" and creates an empty file. touchp
will create the path to the file if it doesn't exist.
fs.touch('to/my/file.txt').then(() => console.log('done!'))
walk
Recursively walks a given path and produces a File tree.
fs.walk('path/to/dir').then( tree_root => {
var everything = tree_root.flatten()
tree_root.children.forEach(node => { /*...*/ })
})
The options include a file filter function which is provided a File
object and must return true to include it in the tree.
examples
// get all '.js' files
var options = {
filter: file => file.isDirectory || file.endsWith('.js')
}
fs.walk('path/to/dir', options).then( tree_root => {
var js_files = tree_root.flatten().filter(file => file.isFile)
})
// get all '.js' file names using `onFile` callback
var js_files = []
var options = {
filter: file => file.isDirectory || file.basename.endsWith('.js'),
onFile: (file, parent) => js_files.push(file.basename)
}
fs.walk('..', options).return(js_files)
API
Generated with dox
File(filepath:String, dirname:String, basename:String)
The file object contains the dirname, basename, children, isFile or isDirectory value, and a stat() function.
File.each(fn:Function, leaves_first:Boolean)
Iterates a callback over all children, if the callback produces a promise it will resolve before subsequent children are iterated over.
If the leaves_first
parameter is truthy the callback will iterate over the tree in a leaf-to-root direction.
File.flatten(filter:Function)
Flattens all File nodes into a single flat array.
walk(root:String, opts:[object Object])
Walk will recursively walk a directory structure creating a file tree as it progresses.
The file tree is a composite of "nodes" where each node is a File
object and may be traversed by the File.children
property;
File.children
is an array of File
objects.
Walk will return the root node once the promised is fulfilled.
options.filter
is a filter function on each node which determines if a node will be included, or excluded, from the file tree.
The filter option may also be an object with a file
and directory
filter function such as { file: f=>true, directory: d=>true }
;
These explicit filter functions are passed either only files or directories respectively.
The onFile
and onDirectory
functions are handler functions which are pass the file or directory, the parent directory, and the options passed to
the walk function (if any).
The filter
, if truthy, will flatten the file tree before it is returned. This may also be a filter function to return only specific Files.
The promissory chain will wait for all `filter`, `onFile`, `onDirectory` callbacks to finish if they return a promise; returning a promise is not necessary.
mkdirp(filepath:String)
Creates all non-existing directories in a root-to-leaf direction after checking if the leaf doesn't exist. The root promise should be fulfilled in a race-tolerant way ( EEXIST are allowed after an ENOENT )
touch(path:String|Buffer, truncate:Boolean, mode:Integer)
Creates a file if it does not exist by opening it with 'a+', or truncating it with 'w+' when the truncate flag is truthy. Will fail if the file cannot be read or written to (EACCESS) or is an existing directory (EISDIR).
removerf(filepath:String)
Recursively removes all files and folders.
Files will be unlinked, and directories are deleted with rmdir from leaf to root.