From 433efce3375170c928050847615fa25f2313d435 Mon Sep 17 00:00:00 2001 From: Daniel Vergeylen Date: Mon, 26 Aug 2019 13:39:20 +0200 Subject: [PATCH 1/4] Added 'depth' option --- index.d.ts | 1 + lib/directory-tree.js | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index fe53fbc..2d38f3f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,7 @@ declare function directoryTree( exclude?: RegExp | RegExp[]; attributes?: string[]; extensions?: RegExp; + depth?: number; }, onEachFile?: (item: directoryTree.DirectoryTree, path: string, stats: directoryTree.Stats) => void, onEachDirectory?: (item: directoryTree.DirectoryTree, path: string, stats: directoryTree.Stats) => void, diff --git a/lib/directory-tree.js b/lib/directory-tree.js index 2e12beb..9665f8c 100644 --- a/lib/directory-tree.js +++ b/lib/directory-tree.js @@ -88,6 +88,10 @@ function directoryTree (path, options, onEachFile, onEachDirectory) { } } else if (stats.isDirectory()) { + if (options && options.depth <= 0) { + return null; + } + let dirData = safeReadDirSync(path); if (dirData === null) return null; @@ -96,8 +100,10 @@ function directoryTree (path, options, onEachFile, onEachDirectory) { item[attribute] = stats[attribute]; }); } + const opts = (options && options.depth) ? options.depth - 1 : options; + item.children = dirData - .map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory)) + .map(child => directoryTree(PATH.join(path, child), opts, onEachFile, onEachDirectory)) .filter(e => !!e); item.size = item.children.reduce((prev, cur) => prev + cur.size, 0); item.type = constants.DIRECTORY; From 87b7d9ee9385d67a63e6c56161773f57196f3a57 Mon Sep 17 00:00:00 2001 From: Daniel Vergeylen Date: Mon, 26 Aug 2019 13:40:00 +0200 Subject: [PATCH 2/4] Added 'depth' option tests --- test/fixtureDepth.js | 67 ++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 5 ++++ 2 files changed, 72 insertions(+) create mode 100644 test/fixtureDepth.js diff --git a/test/fixtureDepth.js b/test/fixtureDepth.js new file mode 100644 index 0000000..9de71a8 --- /dev/null +++ b/test/fixtureDepth.js @@ -0,0 +1,67 @@ +tree = { + "path": "./test/test_data", + "name": "test_data", + "type": "directory", + "children": [ + { + "path": "test/test_data/file_a.txt", + "name": "file_a.txt", + "size": 12, + "type": "file", + "extension": ".txt" + }, + { + "path": "test/test_data/file_b.txt", + "name": "file_b.txt", + "size": 3756, + "type": "file", + "extension": ".txt" + }, + { + "path": "test/test_data/some_dir", + "name": "some_dir", + "type": "directory", + "children": [ + { + "path": "test/test_data/some_dir/another_dir", + "name": "another_dir", + "type": "directory", + "size": 0 + }, + { + "path": "test/test_data/some_dir/file_a.txt", + "name": "file_a.txt", + "size": 12, + "type": "file", + "extension": ".txt" + }, + { + "path": "test/test_data/some_dir/file_b.txt", + "name": "file_b.txt", + "size": 3756, + "type": "file", + "extension": ".txt" + } + ], + "size": 7536 + }, + { + "path": "test/test_data/some_dir_2", + "name": "some_dir_2", + "type": "directory", + "children": [ + { + "path": "test/test_data/some_dir_2/.gitkeep", + "name": ".gitkeep", + "size": 0, + "type": "file", + "extension": "" + } + ], + "size": 0 + } + ], + "size": 11304 +} + +module.exports = tree; diff --git a/test/test.js b/test/test.js index 249975a..5183b78 100644 --- a/test/test.js +++ b/test/test.js @@ -5,6 +5,7 @@ const dirtree = require('../lib/directory-tree'); const testTree = require('./fixture.js'); const excludeTree = require('./fixtureExclude.js') const excludeTree2 = require('./fixtureMultipleExclude.js') +const depthTree = require('./fixtureDepth.js'); describe('directoryTree', () => { @@ -99,4 +100,8 @@ describe('directoryTree', () => { }) }); + it('should not go further than specified depth', () => { + const tree = dirtree('./test/test_data',{ depth:1 }); + expect(tree).to.deep.equal(testTree); + }); }); From f54bc0ba79bda86ace80b0522823174abb464f1b Mon Sep 17 00:00:00 2001 From: Daniel Vergeylen Date: Mon, 26 Aug 2019 13:40:17 +0200 Subject: [PATCH 3/4] Updated README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 11dab40..f9328b8 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ const tree = dirTree('./test/test_data', {extensions:/\.txt$/}, null, (item, PAT `normalizePath` : `Boolean` - If true, windows style paths will be normalized to unix style pathes (/ instead of \\). +`depth` : `Number` - If set, will only render to max depth (e.g: depth 0 is current folder only, depth 1 is current + children folders, ...). + ## Result Given a directory structured like this: From 63000309c67f2f803ea17918612c547f0dadec6d Mon Sep 17 00:00:00 2001 From: Daniel Vergeylen Date: Mon, 26 Aug 2019 15:23:14 +0200 Subject: [PATCH 4/4] Fix tests --- lib/directory-tree.js | 27 ++++++++++++++++----------- test/fixtureDepth.js | 5 +++-- test/test.js | 2 +- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/directory-tree.js b/lib/directory-tree.js index 9665f8c..3ad55db 100644 --- a/lib/directory-tree.js +++ b/lib/directory-tree.js @@ -88,10 +88,6 @@ function directoryTree (path, options, onEachFile, onEachDirectory) { } } else if (stats.isDirectory()) { - if (options && options.depth <= 0) { - return null; - } - let dirData = safeReadDirSync(path); if (dirData === null) return null; @@ -100,15 +96,24 @@ function directoryTree (path, options, onEachFile, onEachDirectory) { item[attribute] = stats[attribute]; }); } - const opts = (options && options.depth) ? options.depth - 1 : options; + const opts = (options && (options.depth >= 0)) + ? { ...options, depth: Number(options.depth) - 1 } + : options; - item.children = dirData - .map(child => directoryTree(PATH.join(path, child), opts, onEachFile, onEachDirectory)) - .filter(e => !!e); - item.size = item.children.reduce((prev, cur) => prev + cur.size, 0); item.type = constants.DIRECTORY; - if (onEachDirectory) { - onEachDirectory(item, PATH, stats); + if (options && (options.depth < 0)) { + if (dirData.length) { + item.children = []; + } + item.size = 0; + } else { + item.children = dirData + .map(child => directoryTree(PATH.join(path, child), opts, onEachFile, onEachDirectory)) + .filter(e => !!e); + item.size = item.children.reduce((prev, cur) => prev + cur.size, 0); + if (onEachDirectory) { + onEachDirectory(item, PATH, stats); + } } } else { return null; // Or set item.size = 0 for devices, FIFO and sockets ? diff --git a/test/fixtureDepth.js b/test/fixtureDepth.js index 9de71a8..c621578 100644 --- a/test/fixtureDepth.js +++ b/test/fixtureDepth.js @@ -26,6 +26,7 @@ tree = { "path": "test/test_data/some_dir/another_dir", "name": "another_dir", "type": "directory", + "children": [], "size": 0 }, { @@ -43,7 +44,7 @@ tree = { "extension": ".txt" } ], - "size": 7536 + "size": 3768 }, { "path": "test/test_data/some_dir_2", @@ -61,7 +62,7 @@ tree = { "size": 0 } ], - "size": 11304 + "size": 7536 } module.exports = tree; diff --git a/test/test.js b/test/test.js index 5183b78..41d0b45 100644 --- a/test/test.js +++ b/test/test.js @@ -102,6 +102,6 @@ describe('directoryTree', () => { it('should not go further than specified depth', () => { const tree = dirtree('./test/test_data',{ depth:1 }); - expect(tree).to.deep.equal(testTree); + expect(tree).to.deep.equal(depthTree); }); });