directory-tree
Advanced tools
Comparing version 2.1.1 to 2.2.0
@@ -6,8 +6,10 @@ declare const directoryTree: ( | ||
exclude?: RegExp | RegExp[]; | ||
attributes?: string[]; | ||
extensions?: RegExp; | ||
}, | ||
onEachFile?: (item: DirectoryTree, path: string) => void | ||
onEachFile?: (item: DirectoryTree, path: string, stats: Stats) => void, | ||
onEachDirectory?: (item: DirectoryTree, path: string, stats: Stats) => void, | ||
) => DirectoryTree; | ||
export declare class DirectoryTree { | ||
declare class DirectoryTree { | ||
path: string; | ||
@@ -21,2 +23,34 @@ name: string; | ||
export default directoryTree; | ||
/* | ||
* Node.js fs.Stats from | ||
* https://github.com/DefinitelyTyped/DefinitelyTyped/blob/fbe90f14d5f6b6d65c4aa78284f212c736078d19/types/node/index.d.ts#L3696 | ||
*/ | ||
declare class Stats { | ||
isFile(): boolean; | ||
isDirectory(): boolean; | ||
isBlockDevice(): boolean; | ||
isCharacterDevice(): boolean; | ||
isSymbolicLink(): boolean; | ||
isFIFO(): boolean; | ||
isSocket(): boolean; | ||
dev: number; | ||
ino: number; | ||
mode: number; | ||
nlink: number; | ||
uid: number; | ||
gid: number; | ||
rdev: number; | ||
size: number; | ||
blksize: number; | ||
blocks: number; | ||
atimeMs: number; | ||
mtimeMs: number; | ||
ctimeMs: number; | ||
birthtimeMs: number; | ||
atime: Date; | ||
mtime: Date; | ||
ctime: Date; | ||
birthtime: Date; | ||
} | ||
export = directoryTree; |
@@ -41,3 +41,12 @@ 'use strict'; | ||
function directoryTree (path, options, onEachFile) { | ||
/** | ||
* Collects the files and folders for a directory path into an Object, subject | ||
* to the options supplied, and invoking optional | ||
* @param {String} path | ||
* @param {Object} options | ||
* @param {function} onEachFile | ||
* @param {function} onEachDirectory | ||
* @return {Object} | ||
*/ | ||
function directoryTree (path, options, onEachFile, onEachDirectory) { | ||
const name = PATH.basename(path); | ||
@@ -60,5 +69,5 @@ path = options && options.normalizePath ? normalizePath(path) : path; | ||
if (stats.isFile()) { | ||
const ext = PATH.extname(path).toLowerCase(); | ||
// Skip if it does not match the extension regex | ||
@@ -71,4 +80,11 @@ if (options && options.extensions && !options.extensions.test(ext)) | ||
item.type = constants.FILE; | ||
if (options && options.attributes) { | ||
options.attributes.forEach((attribute) => { | ||
item[attribute] = stats[attribute]; | ||
}); | ||
} | ||
if (onEachFile) { | ||
onEachFile(item, PATH); | ||
onEachFile(item, PATH, stats); | ||
} | ||
@@ -79,3 +95,8 @@ } | ||
if (dirData === null) return null; | ||
if (options && options.attributes) { | ||
options.attributes.forEach((attribute) => { | ||
item[attribute] = stats[attribute]; | ||
}); | ||
} | ||
item.children = dirData | ||
@@ -86,2 +107,5 @@ .map(child => directoryTree(PATH.join(path, child), options, onEachFile)) | ||
item.type = constants.DIRECTORY; | ||
if (onEachDirectory) { | ||
onEachDirectory(item, PATH, stats); | ||
} | ||
} else { | ||
@@ -88,0 +112,0 @@ return null; // Or set item.size = 0 for devices, FIFO and sockets ? |
{ | ||
"name": "directory-tree", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Convert a directory tree to a JS object.", | ||
@@ -23,3 +23,3 @@ "repository": { | ||
"chai": "^2.3.0", | ||
"mocha": "^2.2.5" | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -26,0 +26,0 @@ "engines": { |
@@ -44,19 +44,34 @@ [![Build Status](https://travis-ci.org/mihneadb/node-directory-tree.svg)](https://travis-ci.org/mihneadb/node-directory-tree) | ||
You can also specify which additional attributes you would like to be included about each file/directory: | ||
```js | ||
const dirTree = require('directory-tree'); | ||
const filteredTree = dirTree('/some/path', {attributes:['mode', 'mtime']}); | ||
``` | ||
The default attributes are `[name, size, extension, path]` for Files and `[name, size, path]` for Directories | ||
A callback function can be executed with each file that matches the extensions provided: | ||
```js | ||
const PATH = require("path"); | ||
const dirTree = require("directory-tree"); | ||
const PATH = require('path'); | ||
const dirTree = require('directory-tree'); | ||
const tree = dirTree( | ||
"./test/test_data", | ||
{ extensions: /\.txt$/ }, | ||
(item, PATH) => { | ||
console.log(item); | ||
} | ||
); | ||
const tree = dirTree('./test/test_data', {extensions:/\.txt$/}, (item, PATH, stats) => { | ||
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). | ||
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) and an instance of [node FS.stats](https://nodejs.org/api/fs.html#fs_class_fs_stats). | ||
You can also pass a callback function for directories: | ||
```js | ||
const PATH = require('path'); | ||
const dirTree = require('directory-tree'); | ||
const tree = dirTree('./test/test_data', {extensions:/\.txt$/}, null, (item, PATH, stats) => { | ||
console.log(item); | ||
}); | ||
``` | ||
## Options | ||
@@ -68,2 +83,4 @@ | ||
`attributes` : `string[]` - Array of [FS.stats](https://nodejs.org/api/fs.html#fs_class_fs_stats) attributes. | ||
`normalizePath` : `Boolean` - If true, windows style paths will be normalized to unix style pathes (/ instead of \\). | ||
@@ -70,0 +87,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
17211
7
375
189
1