Socket
Socket
Sign inDemoInstall

dir-to-tree

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    dir-to-tree

Returns tree objects


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

node-dir-to-tree Build Status

Traverses a directory and returns a tree.

Installation

npm install --save dir-to-tree

Example

var dirToTree = require('dir-to-tree');

dirToTreeSync.sync('some/dir');

Will return an object that looks like this:

{
    relativePath: '',
    children: [
        {
            relativePath: 'app.js'
        },
        {
            relativePath: 'controllers',
            children: [
                {
                    relativePath: 'controllers/application.js'
                },
                {
                    relativePath: 'controllers/user.js'
                }
            ]
        },
        {
            relativePath: 'styles',
            children: [
                {
                    relativePath: 'styles/mixins',
                    children: [
                        {
                            relativePath: 'styles/mixins/_ellipsis.scss'
                        },
                        {
                            relativePath: 'styles/mixins/_rotate.scss'
                        }
                    ]
                },
                {
                    relativePath: 'styles/app.scss'
                }
            ]
        }
    ]
}

Description

dirToTree.sync(dirPath)

Returns a tree that represents dirPath and all it's descendants. All nodes have a relativePath property which is the relative path of the node from dirPath. The root's relativePath is an empty string.

Directories have a children array that contains all the files and directories inside the directory. You can use this property to check whether a node is a directory or not:

if (node.children) {
    //Do thing with directory
} else {
    //Do thing with file
}

Notes

  • There intentionally isn't an asynchronous version. It's not clear that we need or want one. Before sending a patch to add an async version, please share your use case on the issue tracker.

FAQs

Last updated on 07 Oct 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc