read-file-tree

v1.1.2
recursively read contents of all files in a directory
directory fs

read-file-tree

recursively read contents of all files in a directory

npm travis standard

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 to fs.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': '' }
    

License

MIT

npm i read-file-tree

Metadata

  • MIT
  • Whatever
  • Renée Kooi
  • released 10/9/2019

Downloads

Maintainers