Socket
Socket
Sign inDemoInstall

dree

Package Overview
Dependencies
Maintainers
1
Versions
132
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dree

A nodejs module wich helps you handle a directory tree providing you its abstraction through tested functions and a custom configuration.


Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

dree

A nodejs module wich helps you handle a directory tree. It provides you an object of a directory tree with custom configuration and optional callback method when a file or dir is scanned. With Typescript support.

Install

$ npm install dree

Usage

Simple:

const dree = require('dree');
const tree = dree.scan('./folder');

With custom configuration:

const dree = require('dree');

const options = {
  stat: false,
  normalize: true,
  followLinks: true,
  size: true,
  hash: true,
  depth: 5,
  exclude: /nofetchdir/g,
  extensions: [ 'txt', 'jpg' ]
};

const tree = dree.scan('./folder', options);

With file and dir callbacks:

const dree = require('dree');

const options = {
  stat: false
};

const fileCallback = function (element, stat) {
  console.log('Found file named ' + element.name + ' created on ' + stat.ctime);
};
const dirCallback = function (element, stat) {
  console.log('Found file named ' + element.name + ' created on ' + stat.ctime);
};

const tree = dree.scan('./folder', options, fileCallback, dirCallback);

Result

Given a directory structured like this:

sample
├── backend
│   └── firebase.json
│   └── server
│       └── server.ts
└── frontend
│    └── angular
│       ├── app.ts
│       └── index.html
└── .gitignore

With this configurations:

const options = {
    stat: false,
    hash: false,
    sizeInBytes: false,
    size: true,
    normalize: true,
    extensions: [ 'ts', 'json' ]
};

The result will be:

{
  "name": "sample",
  "path": "D:/Github/dree/test/sample",
  "relativePath": ".",
  "type": "directory",
  "isSymbolicLink": false,
  "size": "1.79 MB",
  "children": [
    {
      "name": "backend",
      "path": "D:/Github/dree/test/sample/backend",
      "relativePath": "backend",
      "type": "directory",
      "isSymbolicLink": false,
      "size": "1.79 MB",
      "children": [
        {
          "name": "firebase.json",
          "path": "D:/Github/dree/test/sample/backend/firebase.json",
          "relativePath": "backend/firebase.json",
          "type": "file",
          "isSymbolicLink": false,
          "extension": "json",
          "size": "29 B"
        }, 
        {
          "name": "server",
          "path": "D:/Github/dree/test/sample/backend/server",
          "relativePath": "backend/server",
          "type": "directory",
          "isSymbolicLink": false,
          "size": "1.79 MB",
          "children": [
            {
              "name": "server.ts",
              "path": "D:/Github/dree/test/sample/backend/server/server.ts",
              "relativePath": "backend/server/server.ts",
              "type": "file",
              "isSymbolicLink": false,
              "extension": "ts",
              "size": "1.79 MB"
            }
          ]
        }
      ]
    }
  ]
}

Documentation

TODO

Note

On Windows it could be possible that symbolic links are not detected, due to a problem with node fs module. If symbolicLinks is set to true, then isSymbolicLink could result false for al the tree nodes. In addition, if followLinks is set to true, it could be possible that links will not be followed instead.

The callbacks have a tree representation of the node and its stat as parameters. The tree parameter reflects the options given to scan. For example, if you set hash to false, then the tree parameter of a callback will not have the hash value. The stat parameter depends on the followLinks option. If it is true it will be the result of fs.stat, otherwise it will be the result of fs.lstat.

Properties as hash or size are computed considering only the not filtered nodes. For instance, the result size of a folder could be different from its actual size, if some of its inner files have not been considered due to filters as exclude.

The hash of two nodes with the same content could be different, because also the name is take in consideration.

Dev

To run tests go to the package root in your CLI and run

$ npm test

Make sure you have the dev dependencies installed.

Keywords

FAQs

Package last updated on 07 Feb 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc