-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.mjs
39 lines (34 loc) · 1.07 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { readdir, readFile } from "fs/promises"
import { basename, join } from "path"
async function recursiveFind(dir, keyword, cb) {
const files = await recursion(dir, keyword, 10)
cb(files.flat(Infinity))
}
async function recursion(dir, keyword, parallelism) {
try {
const nestedFiles = await readdir(dir)
const chunks = nestedFiles.reduce((acc, _, index, src) => {
if (index % parallelism) return acc
return [...acc, src.slice(index, index + parallelism)]
}, [])
const allMatches = await new Promise((resolve) => {
chunks.map(async (chunk) => {
const matches = await Promise.all(
chunk.map((nestedFile) =>
recursion(join(dir, nestedFile), keyword, parallelism),
),
)
resolve(matches)
})
})
return allMatches.flat().filter((match) => match)
} catch (err) {
if (err.code === "ENOTDIR") {
const file = await readFile(dir)
if (file.toString().includes(keyword)) {
return basename(dir)
}
}
}
}
recursiveFind("myDir", "batman", console.log)