Skip to content

Commit 7775726

Browse files
committed
add 4.3 solution
1 parent 03aefbe commit 7775726

File tree

12 files changed

+51
-2
lines changed

12 files changed

+51
-2
lines changed

04-callback-patterns/4.2-list-files-recursively.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ function listNestedFiles(dirPath, cb) {
1717
} else {
1818
for (const file of files) {
1919
result.runningOps++;
20-
readDirectory(path.join(dirPath, file), cb);
20+
listNestedFiles(path.join(dirPath, file), cb);
2121
}
2222
}
2323

2424
result.runningOps--;
25-
2625
process.nextTick(() => {
2726
if (!result.runningOps) return cb(null, result.fileList);
2827
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
const result = {
5+
runningOps: 1,
6+
fileList: [],
7+
};
8+
9+
function recursiveFind(dirPath, searchText, cb) {
10+
fs.readdir(dirPath, {}, (err, files) => {
11+
if (err) {
12+
if (err.code !== "ENOTDIR") return cb(err);
13+
searchInFile(dirPath, searchText, cb);
14+
} else {
15+
for (const file of files) {
16+
result.runningOps++;
17+
recursiveFind(path.join(dirPath, file), searchText, cb);
18+
}
19+
20+
finishOp(cb);
21+
}
22+
});
23+
}
24+
25+
function searchInFile(dirPath, searchText, cb) {
26+
fs.readFile(dirPath, "utf8", (err, data) => {
27+
if (err) return cb(err);
28+
29+
if (data.includes(searchText)) result.fileList.push(dirPath);
30+
finishOp(cb);
31+
});
32+
}
33+
34+
function finishOp(cb) {
35+
result.runningOps--;
36+
37+
process.nextTick(() => {
38+
if (!result.runningOps) return cb(null, result.fileList);
39+
});
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
batman snyder cut
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
batman cut
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
snyder
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
batman

04-callback-patterns/DIR/file0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
batman snyder cut

04-callback-patterns/DIR/file1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
snyder cut

04-callback-patterns/DIR/file2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
batman

04-callback-patterns/DIR/file3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cut

04-callback-patterns/DIR/file4.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
snyder

0 commit comments

Comments
 (0)