/
walker.js
92 lines (73 loc) · 2.27 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
/**
* Created by charlie on 9/12/2015.
*/
var walk = require('walk');
var fs = require('fs');
var path = require('path');
var makePage = require('./make-page');
// var utils = require('./utils');
function walker() {
}
// walker.baseDir = '/home/charlie/Git';
walker.fileReport = [];
walker.options = {
followLinks: false,
// directories with these keys will be skipped
filters: ["Temp", "_Temp"]
};
walker.walkDirs = function(directoryToWalk, extensionFilter, callback) {
// var content = process.env.ELF_CONTENT;
// console.log('walk called', walker.content);
var walkInstance = walk.walk(directoryToWalk, walker.options);
walkInstance.on("file", function (root, fileStats, next) {
// console.log('file found', fileStats.name);
var fileExtension = path.extname(fileStats.name);
if(fileExtension === extensionFilter) {
walker.fileReport.push({root: root, fileStats: fileStats});
}
next();
/* fs.readFile(fileStats.name, function () { next(); }); */
});
walkInstance.on("errors", function (root, nodeStatsArray, next) {
console.log(root, nodeStatsArray);
next();
});
walkInstance.on("end", function () {
console.log("all done");
callback(walker.fileReport);
});
};
// for more on mtime: https://nodejs.org/api/fs.html#fs_class_fs_stats
walker.buildFileReport = function(directoryToWalk, extensionFilter, callback) {
walker.walkDirs(directoryToWalk, extensionFilter, function(fileReport) {
var report = fileReport.map(function(file) {
// console.log(file.root);
return {"fileName": file.fileStats.name,
"root": file.root,
"fileSize": file.fileStats.size,
"fileDate": file.fileStats.mtime
};
});
callback(report);
});
};
walker.getDirectories = function(report) {
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 record.root;
});
return directories;
};
walker.makePage = function(directoryToWalk, directories, report) {
console.log("Directories found:", JSON.stringify(directories, null, 4));
directories.forEach(function(directory, index) {
makePage(directory, index, report, directoryToWalk, directories);
});
};
module.exports = walker;