read-file-tree
v1.1.2 directory fs
read-file-tree
recursively read contents of all files in a directory
Install
npm install read-file-tree
Usage
var readFileTree = require('read-file-tree')
readFileTree('/path/to/directory', function (err, tree) {
console.log(tree)
})
API
readFileTree(basedir[, opts], cb)
Recursively read contents of all files in the directory basedir
.
opts
can be an object:
opts.encoding
- encoding to pass tofs.readFile()
, by default a Buffer is returned
cb
is a node-style callback receiving an error
in the first parameter, and a tree
object in the second.
Object keys in the tree
object are file names, while values are the file contents. Nested directories have another tree
object as their value.
For example, the test/fixture directory results in this object:
{ 'one.js': '1;\n',
'two.js': '2;\n',
a: {
b: {
'c.txt': 'this is c\n',
c: {
'd.txt': 'file d\n' } } } }
readFileTree.sync(basedir[, opts])
The same, but sync. Returns tree
.
See Also
- write-file-tree - write an object to nested file tree, with one file for each value
- flat - flatten and unflatten objects—you can use this to create an object with relative paths as keys:
flat(readFileTree('./dest'), { delimiter: '/' }) // { 'a.txt': '', // 'some/dir/name/b.txt': '' }