forked from charliecalvert/isit-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalker.js
134 lines (110 loc) · 3.47 KB
/
walker.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
/**
* Created by charlie on 9/12/2015.
*/
var walk = require('walk');
var fs = require('fs');
var path = require('path');
var elfUtils = require('./elf-utils');
var elfLog = require('./elf-log.js');
var Walker = (function() {
function Walker() {
'use strict';
elfLog.setLevel(elfLog.logLevelSilent);
}
Walker.prototype.directoryToWalk = '';
Walker.prototype.options = {
followLinks: false,
// directories with these keys will be skipped
filters: []
};
function testExtension(extensionFilter, fileExtension) {
'use strict';
if (typeof extensionFilter === 'string') {
return extensionFilter === fileExtension;
} else if (elfUtils.arrayContains(extensionFilter, fileExtension)) {
return true;
} else {
return false;
}
}
Walker.prototype.walkDirs = function(directoryToWalk, extensionFilter, callback) {
'use strict';
var walkerFileReport = [];
this.directoryToWalk = directoryToWalk;
var walkInstance = walk.walk(directoryToWalk, this.options);
walkInstance.on('file', function(root, fileStats, next) {
// console.log('file found', root + path.sep + fileStats.name);
var fileExtension = path.extname(fileStats.name);
if (testExtension(extensionFilter, fileExtension)) {
walkerFileReport.push({
root: root,
fileStats: fileStats
});
}
next();
/* fs.readFile(fileStats.name, function () { next(); }); */
});
walkInstance.on('errors', function(root, nodeStatsArray, next) {
elfLog.log("Error iterating directories");
elfLog.log(elfLog.logLevelDetails, root, nodeStatsArray);
next();
});
walkInstance.on('end', function() {
elfLog.log(elfLog.logLevelDetails, 'all done walking directories');
callback(walkerFileReport);
});
};
Walker.prototype.writeFile = function(fileName, report, response) {
'use strict';
fs.writeFile(fileName, JSON.stringify(report, null, 4), function(err) {
if (err) {
throw (err);
} else {
console.log('wrote file');
response.send({
result: 'success'
});
}
});
};
// for more on mtime: https://nodejs.org/api/fs.html#fs_class_fs_stats
Walker.prototype.getBasics = function(fileReport) {
'use strict';
return fileReport.map(function(file) {
return {
'fileName': file.fileStats.name,
'root': elfUtils.ensureEndsWithPathSep(file.root),
'fileSize': file.fileStats.size,
'fileDate': file.fileStats.mtime
};
});
};
Walker.prototype.getDirectories = function(report) {
'use strict';
var accumulator = [];
var directories = report.filter(function(item, i) {
if (accumulator.indexOf(item.root) === -1) {
accumulator.push(item.root);
return true;
}
}).map(function(record) {
// return { 'root': record.root }
return elfUtils.ensureEndsWithPathSep(record.root);
});
return directories;
};
Walker.prototype.getFileNames = function(report) {
'use strict';
return report.map(function(item) {
return item.fileStats.name;
});
};
Walker.prototype.getFullFileNames = function(report) {
'use strict';
return report.map(function(item) {
return path.normalize(elfUtils.ensureEndsWithPathSep(item.root) + item.fileStats.name);
});
};
return Walker;
})();
module.exports = new Walker();