-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (33 loc) · 860 Bytes
/
index.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
/*
- takes directory path as input
- asynchronously iterates over all directories and sub-directories
- returns list of files found
*/
import fs from 'fs'
import path from 'path'
//Application fields
let arg = process.argv[2]; //get directory
function listNestedFiles (dir, cb) {
readDir(dir,cb)
cb()
}
function readDir(dir,cb){
return fs.readdir(dir,'utf8',(err, files) => {
if(err)
return cb(err)
return files.map(files => path.join(dir,files))
.map(files => checkIfDirectory(files,cb))
})
}
function checkIfDirectory(file,cb){
return fs.stat(file, (err, stat) => {
if(err)
return cb(err)
return stat.isDirectory() == true ? readDir(file,cb) : console.log(file)
})
}
listNestedFiles(arg, err => {
if(err)
console.log(err)
console.log("completed")
})