-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnestedFiles.js
36 lines (31 loc) · 1.03 KB
/
nestedFiles.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
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let directorys = [];
let running = 0;
function nestedFiles(dir, cb) {
readFile(dir, cb)
}
nestedFiles(__dirname + "/../", (err, data) => {
if (err) return console.log("error: ", err);
console.log("list", data);
});
function readFile(directory, cb) {
fs.readdir(directory, { encoding: 'utf8', withFileTypes: true }, (err, dirs) => {
running--
if (!dirs) {
if (!directorys.includes(directory)) directorys.push(directory);
if (running === -1) return cb(null, directorys)
return;
}
for (let i = 0; i < dirs.length; i++) {
const file = dirs[i];
let f = path.join(file.parentPath, file.name)
if (!file.isDirectory() && !directorys.includes(f)) directorys.push(f);
running++
process.nextTick(() => readFile(f, cb))
}
});
}