Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/goto-bus-stop/read-file-tree
recursively read contents of all files in a directory
https://github.com/goto-bus-stop/read-file-tree
directory fs
Last synced: 27 days ago
JSON representation
recursively read contents of all files in a directory
- Host: GitHub
- URL: https://github.com/goto-bus-stop/read-file-tree
- Owner: goto-bus-stop
- License: mit
- Created: 2017-09-16T09:21:47.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-31T04:56:39.000Z (over 1 year ago)
- Last Synced: 2024-10-08T04:04:34.962Z (about 1 month ago)
- Topics: directory, fs
- Language: JavaScript
- Size: 52.7 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# read-file-tree
recursively read contents of all files in a directory
[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url][npm-image]: https://img.shields.io/npm/v/read-file-tree.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/read-file-tree
[travis-image]: https://img.shields.io/travis/goto-bus-stop/read-file-tree.svg?style=flat-square
[travis-url]: https://travis-ci.org/goto-bus-stop/read-file-tree
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/standard## Install
```
npm install read-file-tree
```## Usage
```js
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()`](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback), 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](./test/fixture) directory results in this object:```js
{ '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](https://github.com/goto-bus-stop/write-file-tree) - write an object to nested file tree, with one file for each value
* [flat](https://github.com/hughsk/flat) - flatten and unflatten objects—you can use this to create an object with relative paths as keys:
```js
flat(readFileTree('./dest'), { delimiter: '/' })
// { 'a.txt': '',
// 'some/dir/name/b.txt': '' }
```## License
[MIT](LICENSE.md)