directory-tree
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -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 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4664
37
130