Skip to content

Recursive #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ declare function directoryTree(
exclude?: RegExp | RegExp[];
attributes?: (keyof directoryTree.Stats)[];
extensions?: RegExp;
recursionDepth: number
},
onEachFile?: (item: directoryTree.DirectoryTree, path: string, stats: directoryTree.Stats) => void,
onEachDirectory?: (item: directoryTree.DirectoryTree, path: string, stats: directoryTree.Stats) => void,
Expand Down
15 changes: 13 additions & 2 deletions lib/directory-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ function isRegExp(regExp) {
* @return {Object}
*/
function directoryTree (path, options, onEachFile, onEachDirectory) {
return directoryTreeInternal(path, options, onEachFile, onEachDirectory, 0);
}

function directoryTreeInternal (path, options, onEachFile, onEachDirectory, currentDepth) {
const name = PATH.basename(path);
path = options && options.normalizePath ? normalizePath(path) : path;
const item = { path, name };
Expand All @@ -66,7 +70,6 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
}

if (stats.isFile()) {

const ext = PATH.extname(path).toLowerCase();

// Skip if it does not match the extension regex
Expand Down Expand Up @@ -96,8 +99,16 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
item[attribute] = stats[attribute];
});
}

if((options && options.recursionDepth) && currentDepth >= options.recursionDepth) {
item.children = []; // To indicate there is more children but we wont read them
item.type = constants.DIRECTORY;
item.size = null; // Directories are set to size null ?
return item;
}

item.children = dirData
.map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory))
.map(child => directoryTreeInternal(PATH.join(path, child), options, onEachFile, onEachDirectory, currentDepth + 1))
.filter(e => !!e);
item.size = item.children.reduce((prev, cur) => prev + cur.size, 0);
item.type = constants.DIRECTORY;
Expand Down
Empty file added test/depth_test/depth1_file
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
40 changes: 40 additions & 0 deletions test/fixtureDepth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
tree = {
"path": "./test/depth_test",
"name": "depth_test",
"children": [
{
"path": "test/depth_test/depth1_file",
"name": "depth1_file",
"size": 0,
"extension": "",
"type": "file"
},
{
"path": "test/depth_test/depth2",
"name": "depth2",
"children": [
{
"path": "test/depth_test/depth2/depth2_file",
"name": "depth2_file",
"size": 0,
"extension": "",
"type": "file"
},
{
"path": "test/depth_test/depth2/depth3",
"name": "depth3",
"children": [],
"type": "directory",
"size": null
}
],
"size": 0,
"type": "directory"
}
],
"size": 0,
"type": "directory"
}


module.exports = tree;
18 changes: 11 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const dirtree = require('../lib/directory-tree');
const testTree = require('./fixture.js');
const excludeTree = require('./fixtureExclude.js')
const excludeTree2 = require('./fixtureMultipleExclude.js')

const depthTestTree = require('./fixtureDepth.js')

describe('directoryTree', () => {

Expand All @@ -17,12 +17,12 @@ describe('directoryTree', () => {
it('should list the children in a directory', () => {
const tree = dirtree('./test/test_data', {extensions:/\.txt$/});

// 4 including the empty `some_dir_2`.
expect(tree.children.length).to.equal(4);
// 5 including the empty `some_dir_2`.
expect(tree.children.length).to.equal(5);
});

it('should execute a callback function for each file with no specified extensions', () => {
let number_of_files = 7;
let number_of_files = 8;
let callback_executed_times = 0;

const tree = dirtree('./test/test_data', null, function(item, PATH) {
Expand All @@ -33,7 +33,7 @@ describe('directoryTree', () => {
});

it('should execute a callback function for each directory', () => {
let number_of_directories = 4;
let number_of_directories = 9;
let callback_executed_times = 0;

const tree = dirtree('./test/test_data', null, null, function(item, PATH) {
Expand Down Expand Up @@ -79,12 +79,12 @@ describe('directoryTree', () => {
})

it('should exclude the correct folders', () => {
const tree = dirtree('./test/test_data',{exclude: /another_dir/, normalizePath: true});
const tree = dirtree('./test/test_data',{exclude: [/another_dir/, /depth1/], normalizePath: true});
expect(tree).to.deep.equal(excludeTree);
});

it('should exclude multiple folders', () => {
const tree = dirtree('./test/test_data', {exclude: [/another_dir/, /some_dir_2/], normalizePath: true});
const tree = dirtree('./test/test_data', {exclude: [/another_dir/, /some_dir_2/, /depth1/], normalizePath: true});
expect(tree).to.deep.equal(excludeTree2);

});
Expand All @@ -99,4 +99,8 @@ describe('directoryTree', () => {
})
});

it('should return exact', () => {
const tree = dirtree('./test/test_data/depth1', { recursionDepth: 4 });
expect(tree).to.deep.equal(depthTestTree);
});
});