-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_dir.js
85 lines (59 loc) · 1.9 KB
/
list_dir.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var fs = require('fs');
var path_ = require("path");
function listdir(path,callback){
//check if the path exist and its not a file
list_of_files=[]
fs.lstat(path, (err, stats) => {
if (err){
callback(err,null);
return;
}
if (!stats.isDirectory())
{
error=new Error('Path is not a directory.');
error.code='NOTDIR';
callback(error,null);
return;
}
fs.readdir(path, (err, files) => {
if (err){
callback(err,null);
return;
}
counter=0;
error_flag=null
var parentDir = path_.join(path, '../');
list_of_files.push({type:'folder',name:'<-',size:0,path:parentDir,currDir:path});
if (files.length==0)
{
callback(null,list_of_files);
}
files.forEach(file => {
fs.lstat(path+'\\'+file, (err, stats) => {
counter++;
if (error_flag && counter===files.length)
{
callback(error_flag,null);
return;
}
if(err || error_flag)
{
if (err)
error_flag=err
return;
}
if (stats.isFile())
list_of_files.push({type:'file',name:file,size:Math.floor(stats.size/1024)+' KB',path:path+'\\'+file})
else if (stats.isDirectory())
list_of_files.push({type:'folder',name:file,size:stats.size+' KB',path:path+'\\'+file})
if (counter===files.length)
{
callback(null,list_of_files);
return;
}
});
});
})
});
}
module.exports.listdir=listdir