Skip to content
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

Default attributes only "name", "path" and "children" #93

Merged
merged 5 commits into from
Sep 15, 2021
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
- run: npm test
42 changes: 31 additions & 11 deletions lib/directory-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
options = { ...options, symlinks: [] };
// Skip if a cyclic symbolic link has been found
if (options.symlinks.find(ino => ino === lstat.ino)) {
return null;
return null;
} else {
options.symlinks.push(lstat.ino);
}
Expand All @@ -94,13 +94,20 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
if (options.extensions && !options.extensions.test(ext))
return null;

item.size = stats.size; // File size in bytes
item.extension = ext;
item.type = constants.FILE;

if (options.attributes) {
options.attributes.forEach((attribute) => {
item[attribute] = stats[attribute];
switch (attribute) {
case 'extension':
item.extension = ext;
break;
case 'type':
item.type = constants.FILE;
break;
default:
item[attribute] = stats[attribute];
break;
}
});
}

Expand All @@ -112,16 +119,29 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
let dirData = safeReadDirSync(path);
if (dirData === null) return null;

item.children = dirData
.map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory))
.filter(e => !!e);

if (options.attributes) {
options.attributes.forEach((attribute) => {
item[attribute] = stats[attribute];
switch (attribute) {
case 'size':
item.size = item.children.reduce((prev, cur) => prev + cur.size, 0);
break;
case 'type':
item.type = constants.DIRECTORY;
break;
case 'extension':
break;
default:
item[attribute] = stats[attribute];
break;
}

});
}
item.children = dirData
.map(child => directoryTree(PATH.join(path, child), options, 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);
}
Expand Down
10 changes: 5 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('directoryTree', () => {
});

it('should display the size of a directory (summing up the children)', () => {
const tree = dirtree('./test/test_data', {extensions:/\.txt$/});
const tree = dirtree('./test/test_data', {extensions:/\.txt$/, attributes: ['size']});
expect(tree.size).to.be.above(11000);
});

Expand All @@ -65,7 +65,7 @@ describe('directoryTree', () => {
});

it('should return the correct exact result', () => {
const tree = dirtree('./test/test_data', {normalizePath: true, followSymlinks: false });
const tree = dirtree('./test/test_data', {normalizePath: true, followSymlinks: false, attributes: ['size','type','extension'] });
expect(tree).to.deep.equal(testTree);
});

Expand All @@ -80,12 +80,12 @@ describe('directoryTree', () => {
})

it('should exclude the correct folders', () => {
const tree = dirtree('./test/test_data',{exclude: /another_dir/, normalizePath: true, followSymlinks: false});
const tree = dirtree('./test/test_data',{exclude: /another_dir/, normalizePath: true, followSymlinks: false, attributes: ['size','type','extension']});
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, followSymlinks: false});
const tree = dirtree('./test/test_data', {exclude: [/another_dir/, /some_dir_2/], normalizePath: true, followSymlinks: false, attributes: ['size','type','extension']});
expect(tree).to.deep.equal(excludeTree2);

});
Expand All @@ -101,7 +101,7 @@ describe('directoryTree', () => {
});

it('should follow symlinks', () => {
const tree = dirtree('./test/test_data', {normalizePath: true, followSymlinks: true });
const tree = dirtree('./test/test_data', {normalizePath: true, followSymlinks: true, attributes: ['size','type','extension'] });
expect(tree).to.deep.equal(symlinkTree);
})

Expand Down