forked from clonn/course-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_asyncReadDir.js
49 lines (40 loc) · 1010 Bytes
/
util_asyncReadDir.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
const {promisify} = require('util');
let fs = require('fs');
let path = require('path');
const basePath = './';
let readdir = promisify(fs.readdir);
let stat = promisify(fs.stat);
const isDir = async (subPath) => {
let pathResult = await stat(subPath);
if (pathResult.isDirectory()) {
return subPath;
} else {
throw('data is not a directory');
}
};
let dirs = async () => {
return await readdir(basePath, {encoding: 'utf8', withFileTypes: true})
};
async function start () {
try {
let folders = await dirs();
let fullPaths = [];
folders.map(async (dir) => {
let subPath = path.join(basePath, dir);
fullPaths.push(subPath);
});
folders.map(async (dir) => {
try {
let subPath = await isDir(dir);
fs.readdir(subPath, (error, fileName) => {
console.log('SUCCESS in dir,', fileName);
});
} catch (e) {
console.warn(e);
}
});
} catch (e) {
console.warn(e);
}
}
start();