-
Notifications
You must be signed in to change notification settings - Fork 106
/
directory-tree.js
161 lines (140 loc) · 4.11 KB
/
directory-tree.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'use strict';
const FS = require('fs');
const PATH = require('path');
const constants = {
DIRECTORY: 'directory',
FILE: 'file'
}
function safeReadDirSync (path) {
let dirData = {};
try {
dirData = FS.readdirSync(path);
} catch(ex) {
if (ex.code == "EACCES" || ex.code == "EPERM") {
//User does not have permissions, ignore directory
return null;
}
else throw ex;
}
return dirData;
}
/**
* Normalizes windows style paths by replacing double backslahes with single forward slahes (unix style).
* @param {string} path
* @return {string}
*/
function normalizePath(path) {
return path.replace(/\\/g, '/');
}
/**
* Tests if the supplied parameter is of type RegExp
* @param {any} regExp
* @return {Boolean}
*/
function isRegExp(regExp) {
return typeof regExp === "object" && regExp.constructor == RegExp;
}
/**
* Collects the files and folders for a directory path into an Object, subject
* to the options supplied, and invoking optional
* @param {String} path
* @param {Object} options
* @param {function} onEachFile
* @param {function} onEachDirectory
* @return {Object}
*/
function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth = 0) {
options = options || {};
if (options.depth !== undefined && options.attributes && options.attributes.indexOf('size') !== -1) {
throw new Error('usage of size attribute with depth option is prohibited');
}
const name = PATH.basename(path);
path = options.normalizePath ? normalizePath(path) : path;
const item = { path, name };
let stats;
let lstat;
try {
stats = FS.statSync(path);
lstat = FS.lstatSync(path);
}
catch (e) { return null }
// Skip if it matches the exclude regex
if (options.exclude) {
const excludes = isRegExp(options.exclude) ? [options.exclude] : options.exclude;
if (excludes.some((exclusion) => exclusion.test(path))) {
return null;
}
}
if (lstat.isSymbolicLink()) {
item.isSymbolicLink = true;
// Skip if symbolic links should not be followed
if (options.followSymlinks === false)
return null;
// Initialize the symbolic links array to avoid infinite loops
if (!options.symlinks)
options = { ...options, symlinks: [] };
// Skip if a cyclic symbolic link has been found
if (options.symlinks.find(ino => ino === lstat.ino)) {
return null;
} else {
options.symlinks.push(lstat.ino);
}
}
if (stats.isFile()) {
const ext = PATH.extname(path).toLowerCase();
// Skip if it does not match the extension regex
if (options.extensions && !options.extensions.test(ext))
return null;
if (options.attributes) {
options.attributes.forEach((attribute) => {
switch (attribute) {
case 'extension':
item.extension = ext;
break;
case 'type':
item.type = constants.FILE;
break;
default:
item[attribute] = stats[attribute];
break;
}
});
}
if (onEachFile) {
onEachFile(item, path, stats);
}
}
else if (stats.isDirectory()) {
let dirData = safeReadDirSync(path);
if (dirData === null) return null;
if (options.depth === undefined || options.depth > currentDepth) {
item.children = dirData
.map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory, currentDepth + 1))
.filter(e => !!e);
}
if (options.attributes) {
options.attributes.forEach((attribute) => {
switch (attribute) {
case 'size':
item.size = item.children.reduce((prev, cur) => prev + cur.size, 0);
break;
case 'type':
item.type = constants.DIRECTORY;
break;
case 'extension':
break;
default:
item[attribute] = stats[attribute];
break;
}
});
}
if (onEachDirectory) {
onEachDirectory(item, path, stats);
}
} else {
return null; // Or set item.size = 0 for devices, FIFO and sockets ?
}
return item;
}
module.exports = directoryTree;