directory-tree-promise

2.0.1 • Public • Published

directory-tree-promise Build Status

Install

  npm install directory-tree-promise

Usage

Use async await while traversing a directory tree.

  const directoryTree = require('directory-tree-promise')
  const tree = await directoryTree('./example')

Output of tree:

{
  "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

  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

  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

Package Sidebar

Install

npm i directory-tree-promise

Weekly Downloads

10

Version

2.0.1

License

MIT

Unpacked Size

16 kB

Total Files

12

Last publish

Collaborators

  • dannav