Skip to content

Commit

Permalink
fix: fdir never resolves when maxDepth < 0 (fixes #127)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Oct 16, 2024
1 parent e17009b commit abeacfb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
9 changes: 9 additions & 0 deletions __tests__/fdir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ for (const type of apiTypes) {
).toBeTruthy();
});

test(`[${type}] crawl and get all files (withMaxDepth = -1)`, async (t) => {
const api = new fdir()
.withMaxDepth(-1)
.withBasePath()
.crawl("node_modules");
const files = await api[type]();
t.expect(files.length).toBe(0);
});

test(`[${type}] crawl and get files that match a glob pattern`, async (t) => {
const api = new fdir()
.withBasePath()
Expand Down
2 changes: 1 addition & 1 deletion src/api/functions/walk-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const walkAsync: WalkDirectoryFunction = (
currentDepth,
callback
) => {
if (currentDepth < 0) return;
if (currentDepth < 0) return state.queue.dequeue(null, state);

state.visited.push(crawlPath);
state.counts.directories++;
Expand Down
40 changes: 20 additions & 20 deletions src/api/queue.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { WalkerState } from "../types";

type OnQueueEmptyCallback = (error: Error | null, output: WalkerState) => void;
/**
* This is a custom stateless queue to track concurrent async fs calls.
* It increments a counter whenever a call is queued and decrements it
* as soon as it completes. When the counter hits 0, it calls onQueueEmpty.
*/
export class Queue {
private count: number = 0;
constructor(private readonly onQueueEmpty: OnQueueEmptyCallback) {}

enqueue() {
this.count++;
}

dequeue(error: Error | null, output: WalkerState) {
if (--this.count === 0 || error) this.onQueueEmpty(error, output);
}
}
import { WalkerState } from "../types";

type OnQueueEmptyCallback = (error: Error | null, output: WalkerState) => void;
/**
* This is a custom stateless queue to track concurrent async fs calls.
* It increments a counter whenever a call is queued and decrements it
* as soon as it completes. When the counter hits 0, it calls onQueueEmpty.
*/
export class Queue {
private count: number = 0;
constructor(private readonly onQueueEmpty: OnQueueEmptyCallback) {}

enqueue() {
this.count++;
}

dequeue(error: Error | null, output: WalkerState) {
if (--this.count <= 0 || error) this.onQueueEmpty(error, output);
}
}

0 comments on commit abeacfb

Please sign in to comment.