This repository was archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlinecount.js
83 lines (70 loc) · 2.03 KB
/
linecount.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
var os = require('os')
, fs = require('fs')
, parser = require('./parser');
function walkRepo(path, onFile) {
if(fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file,index){
if (file.indexOf(".git") != 0) { // skip git-related files and dirs
var curPath = path + "/" + file;
if (fs.statSync(curPath).isDirectory()) { // recurse
walkRepo(curPath, onFile);
} else { // process file and update accumulated result
onFile(curPath);
}
}
});
}
}
function addResult(acc, newResult) {
if (newResult) {
if (acc.hasOwnProperty(newResult.filetype)) {
acc[newResult.filetype].files += 1;
acc[newResult.filetype].lines += newResult.lines;
acc[newResult.filetype].codeLines += newResult.codeLines;
} else {
acc[newResult.filetype] = {
files: 1,
lines: newResult.lines,
codeLines: newResult.codeLines
};
}
}
}
function getFilename(path) {
var lastSlash = path.lastIndexOf('/');
if (lastSlash > -1) {
return path.substring(lastSlash + 1);
} else {
return path;
}
}
function countLines(rootDir, socket, onComplete) {
socket.emit("console-output", "Counting lines...");
var filesRemaining = 0;
var results = {};
var walkComplete = false;
var onFileParseResult = function(fileResult) {
addResult(results, fileResult);
filesRemaining -= 1;
if (walkComplete && filesRemaining == 0) {
// all files have been parsed, so we're done
socket.emit("results", results);
onComplete();
}
};
var onFile = function(path) {
filesRemaining += 1;
socket.emit("console-output", "Parsing file: " + getFilename(path));
parser.parse(path, onFileParseResult);
};
walkRepo(rootDir, onFile);
walkComplete = true;
}
var LineCount = {
countLinesBuilder: function(onComplete) {
return function(rootDir, socket) {
countLines(rootDir, socket, onComplete);
}
}
};
module.exports = LineCount;