Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

directory-tree

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

directory-tree - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

14

lib/directory-tree.js

@@ -6,3 +6,3 @@ 'use strict';

function directoryTree (path, extensions) {
function directoryTree (path, extensions, onEachFile) {
const name = PATH.basename(path);

@@ -17,5 +17,13 @@ const item = { path, name };

const ext = PATH.extname(path).toLowerCase();
if (extensions && extensions.length && extensions.indexOf(ext) === -1) return null;
// Only add files with the provided extensions
if (extensions && extensions.length && extensions.indexOf(ext) === -1)
return null;
item.size = stats.size; // File size in bytes
item.extension = ext;
if (onEachFile) {
onEachFile(item, PATH);
}
}

@@ -25,3 +33,3 @@ else if (stats.isDirectory()) {

item.children = FS.readdirSync(path)
.map(child => directoryTree(PATH.join(path, child), extensions))
.map(child => directoryTree(PATH.join(path, child), extensions, onEachFile))
.filter(e => !!e);

@@ -28,0 +36,0 @@ item.size = item.children.reduce((prev, cur) => prev + cur.size, 0);

{
"name": "directory-tree",
"version": "1.1.1",
"version": "1.2.0",
"description": "Convert a directory tree to a JS object.",

@@ -5,0 +5,0 @@ "repository": {

@@ -28,4 +28,15 @@ [![Build Status](https://travis-ci.org/mihneadb/node-directory-tree.svg)](https://travis-ci.org/mihneadb/node-directory-tree)

This will take a directory tree:
A callback function can be executed with each file that matches the extensions provided:
```js
var dirTree = require('directory-tree');
var tree = dirtree('./test/test_data', ['.jpg'], function(item, PATH) {
console.log(item);
});
```
The callback function takes the directory item (has path, name, size, and extension) and an instance of [node path](https://nodejs.org/api/path.html)
##Result
Given a directory structured like this:
```

@@ -42,3 +53,3 @@ photos

And return a js object:
directory-tree will return this js object:

@@ -45,0 +56,0 @@ ```json

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