Skip to content

Commit d7f7aca

Browse files
committed
Fix exclude directories bug
1 parent 813d401 commit d7f7aca

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

__tests__/__snapshots__/tree.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ exports[`tree directoriesOnly 5`] = `
8686
│ ├── alpha
8787
│ │ └── beta
8888
│ └── charlie
89-
── charlie
90-
── beta"
89+
── charlie
90+
── beta"
9191
`;
9292

9393
exports[`tree directoriesOnly 6`] = `"single-file"`;

tree.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,20 @@ function print(
7373
}
7474

7575
// Handle directory files.
76-
const files = fs.readdirSync(path)
76+
let files = fs.readdirSync(path)
7777
.filter(
7878
file => !EXCLUSIONS.includes(file)
7979
);
80+
if (options.directoriesOnly) {
81+
// We have to filter here instead of at the start of the function
82+
// because we need to know how many non-directories there are before
83+
// we even start recursing.
84+
files = files.filter(file => {
85+
const filePath = nodePath.join(path, file);
86+
return !fs.lstatSync(filePath).isFile();
87+
})
88+
}
89+
8090
files.forEach((file, index) => {
8191
const isCurrentLast = index === files.length - 1;
8292
const linesForFile = print(
@@ -103,9 +113,3 @@ function tree(path, options) {
103113
}
104114

105115
module.exports = tree;
106-
107-
// console.log(
108-
// printTree('/Users/yangshun/Developer/redux-devtools/', {
109-
// excludeDirs: ['node_modules'],
110-
// }),
111-
// );

0 commit comments

Comments
 (0)