Skip to content

Commit dcee48b

Browse files
committed
cleanup and comments
1 parent 92811f1 commit dcee48b

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

utils/files.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,7 @@ function readFile (file, callback) {
6565
}
6666
}
6767

68-
function harmonizeAndFilter(dir) {
69-
var filesList = [];
70-
for (var i = 0; i < dir.length; i++) {
71-
var fileObj = dir[i];
72-
fileObj.relativePath = harmonizeRelativePath(fileObj.webkitRelativePath);
73-
if (type.isIgnoredPath(fileObj.relativePath)) {
74-
continue;
75-
}
76-
filesList.push(fileObj);
77-
}
78-
return filesList;
79-
}
68+
8069

8170
/**
8271
* Read Directory
@@ -92,11 +81,9 @@ function readDir (dir, callback) {
9281
var filesObj = {};
9382
var filesList = [];
9483
if (fs) {
95-
var str = dir.substr(dir.lastIndexOf('/') + 1) + '$';
96-
var rootpath = dir.replace(new RegExp(str), '');
97-
filesList = getFiles(dir, [], rootpath);
84+
filesList = preprocessNode(dir);
9885
} else {
99-
filesList = harmonizeAndFilter(dir);
86+
filesList = preprocessBrowser(dir);
10087
}
10188
// converting array to object
10289
for (var j = 0; j < filesList.length; j++) {
@@ -105,7 +92,42 @@ function readDir (dir, callback) {
10592
callback(filesObj);
10693
}
10794

108-
function getFiles (dir, files_, rootpath){
95+
/**
96+
* Preprocess file objects from a browser
97+
*
98+
* 1. Filters out ignored files and folder.
99+
* 2. Adds 'relativePath' field of each file object.
100+
*/
101+
function preprocessBrowser(filesObj) {
102+
var filesList = [];
103+
for (var i = 0; i < filesObj.length; i++) {
104+
var fileObj = filesObj[i];
105+
fileObj.relativePath = harmonizeRelativePath(fileObj.webkitRelativePath);
106+
if (type.isIgnoredPath(fileObj.relativePath)) {
107+
continue;
108+
}
109+
filesList.push(fileObj);
110+
}
111+
return filesList;
112+
}
113+
114+
/**
115+
* Preprocess directory path from a Node CLI
116+
*
117+
* 1. Recursively travers the directory tree
118+
* 2. Filters out ignored files and folder.
119+
* 3. Harmonizes the 'relativePath' field
120+
*/
121+
function preprocessNode(dir) {
122+
var str = dir.substr(dir.lastIndexOf('/') + 1) + '$';
123+
var rootpath = dir.replace(new RegExp(str), '');
124+
return getFiles(dir, [], rootpath);
125+
}
126+
127+
/**
128+
* Recursive helper function for 'preprocessNode'
129+
*/
130+
function getFiles(dir, files_, rootpath){
109131
files_ = files_ || [];
110132
var files = fs.readdirSync(dir);
111133
for (var i = 0; i < files.length; i++) {

0 commit comments

Comments
 (0)