node-filewalker icon indicating copy to clipboard operation
node-filewalker copied to clipboard

[bug] Keep counting directories when recursion is disabled

Open thiagodp opened this issue 7 years ago • 0 comments

Method _stat calls _emitDir when a directory is found, even when recursion is disabled, and this makes the counter dirs to accumulate the number of analyzed directories.

For instance, let's suppose that we have a directory foo with the subdirectories bar and zoo:

foo/
    |_ bar/
         |_ file1.jpg
    |_ zoo/
    |_ file1.txt
    |_ file2.txt

A recursive walk in foo makes filewalker.dirs to be 3, correctly. However, a non-recursive walk in foo also makes filewalker.dirs to be 3, instead of being 1.

Here the method _stat compares whether the entry is a directory and then it calls _emitDir. I think it can be changed to the following comparison:

if ( s.isDirectory() && ( this.recursive || ! this.recursive && this.dirs < 1 ) ) {
   // ... (same as currently)
}

thiagodp avatar Jan 23 '18 01:01 thiagodp