-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursiveFind.js
58 lines (52 loc) · 1.7 KB
/
recursiveFind.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import fs from 'fs'
import { fileURLToPath } from 'url'
import path from 'path';
let arrayToSearch = [];
let tasks = [];
let running = 0;
const __diname = path.dirname(fileURLToPath(import.meta.url));
function recursiveFind(dir, keyword, cb) {
tasks.push(() => {
readFile(dir, keyword, cb)
})
while (tasks.length) {
let task = tasks.shift();
task()
}
}
function searchKeyword(keyword, cb) {
let coincidence = []
arrayToSearch.forEach(file => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) cb(err)
if (data.match(keyword)) coincidence.push({ file, data })
arrayToSearch.shift();
if (arrayToSearch.length === 0) return cb(null, coincidence)
})
})
}
function readFile(dir, keyword, cb) {
fs.readdir(dir, { withFileTypes: true }, (err, files) => {
running--
if (err) return cb(err)
if (!files) return;
files.forEach(file => {
let _file = path.join(file.parentPath, file.name);
running++
if (!file.isDirectory()) {
arrayToSearch.push(_file)
running--
if (!files.some(e => e.isDirectory()) && running === -1) return searchKeyword(keyword, cb)
return
}
return recursiveFind(_file, keyword, cb);
})
})
}
let pathToSearch = String(process.argv[2])
let keyword = String(process.argv[3])
recursiveFind(__diname + pathToSearch, keyword, (err, coincidence) => {
if (err) return console.log('error', err);
if (coincidence.length > 0) return console.log('I found', coincidence)
else return console.log('Nothing was found', coincidence)
})