https://github.com/dannav/directory-tree-promise
Asynchronously return a directory tree as a JS object
https://github.com/dannav/directory-tree-promise
Last synced: 5 months ago
JSON representation
Asynchronously return a directory tree as a JS object
- Host: GitHub
- URL: https://github.com/dannav/directory-tree-promise
- Owner: dannav
- License: mit
- Created: 2017-03-11T21:47:27.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-17T19:13:38.000Z (almost 7 years ago)
- Last Synced: 2024-11-09T20:04:47.908Z (6 months ago)
- Language: JavaScript
- Size: 15.6 KB
- Stars: 5
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## directory-tree-promise [](https://travis-ci.org/dannav/directory-tree-promise)
### Install
```
npm install directory-tree-promise
```### Usage
Use async await while traversing a directory tree.
```js
const directoryTree = require('directory-tree-promise')
const tree = await directoryTree('./example')
```Output of `tree`:
```json
{
"path": "./example",
"name": "example",
"children": [
{
"path": "example/subfolder",
"name": "subfolder",
"children": [
// excluded
],
"size": 22
},
{
// excluded
}
],
"size": 34
}
```You can also pass an async function as a second parameter to modify the object returned for files
```js
const directoryTree = require('directory-tree-promise')
const tree = await directoryTree('./example', async file => {
file.content = await someFunctionToReadFileData(file.path)
return file
})
```### Object Property Reference
Items are returned with the following properties:
- path (path to item)
- name (name of file including extension)
- extension (extension of object if isFile)
- size (file or folder size in bytes)
- children (array of items)### API Reference
#### isFile [bool]
Each object in the tree is extended with a property isFile
```js
const directoryTree = require('directory-tree-promise')
const tree = await directoryTree(process.cwd())
tree.children[0].isFile // true or false
```### Note
Device, FIFO and socket files are ignored.
### Contributing
Fork this repository and run `npm install` in project directory. Before versioning and publishing, run `npm dist` to transcompile for Node 6 support.
#### Tests
`npm run test`