-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistNestedFiles.mjs
77 lines (60 loc) · 1.77 KB
/
listNestedFiles.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { readdir, writeFile } from 'fs';
import { promisify } from 'util';
/**
* ### 로컬 파일시스템의 디렉터리경로를 입력으로 받으며 비동기적으로 반복하여 발견되는 모든 서브 디렉터리를 비동기적으로 반환.
* - 콜백 지옥을 가능하면 피할것.
* - 필요하다면 헬퍼함수를 자유롭게 만들어 사용해도 됨.
*
* ### => 나는, 객체로 트리구조 표현해서 콜백에 전달하도록 했다.
* @param {string} dir
* @param {(err, dirObj?) => any} cb
* @returns {void}
*/
function listNestedFiles(dir, cb) {
const works = new Set();
const rootDirObj = {};
const next = (nextDir, nextObj, nextCb) => {
works.add(nextDir);
readdir(nextDir, {withFileTypes: true}, (err, files) => {
if (err) {
return cb(err);
};
files
.filter(f => f.isDirectory())
.map(f => f.name)
.filter(f => f.charAt(0) !== '.') // 숨김폴더 제외
.forEach(f => {
nextObj[f] = {};
next(`${nextDir}/${f}`, nextObj[f], (err) => {
if (err) {
return cb(err);
};
});
});
nextCb(null, nextObj);
works.delete(nextDir);
if (works.size === 0) {
cb(null, rootDirObj);
}
});
};
next(dir, rootDirObj, (err, obj) => {
if (err) {
return cb(err);
};
});
}
//--------------------------------------------------------------------------------
// 실행 하는 부분은 promisify 이용해보았다.
promisify(listNestedFiles)('../')
.then(result => {
promisify(writeFile)(
'./listNestedFiles.json',
JSON.stringify(result, null, 2)
).then(
() => console.log('done'),
err => console.error(err)
);
}).catch(err => {
console.error(err);
});