This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
/
stats.js
129 lines (111 loc) · 5.59 KB
/
stats.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
/*
* Copyright 2011 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var _c = require('./conf'),
_path = require('path');
module.exports = function () {
var libs = [],
tests = [],
total_lines = 0,
total_loc = 0,
lib_loc = 0,
lib_lines = 0,
test_loc = 0,
test_lines = 0,
emptySpace,
testsOverLib;
function spaces(number) {
var str = "", i;
for (i = 0; i < number; i++) {
str += " ";
}
return str;
}
function parseFile(file, callback) {
var lines = 0,
loc = 0,
projectroot = _path.resolve(_path.join(__dirname, ".."));
if (file.match(/\.js$/)) {
// hack!
require('fs').readFileSync(file, "utf-8").replace(/\n$/, '').split("\n").forEach(function (line) {
lines++;
if (line !== "" && !line.match(/^\s*(\/\/.*)?$/)) {
loc++;
}
});
file = file.replace(/\.js$/, '').replace(new RegExp(projectroot + "\/?"), '');
process.stdout.write("| " + file + spaces(74 - file.length) + "| " +
lines + spaces(7 - String(lines).length) + "| " +
loc + spaces(7 - String(loc).length) + "|\n");
callback(lines, loc);
}
}
function collect(path, files) {
var fs = require('fs');
if (fs.statSync(path).isDirectory()) {
fs.readdirSync(path).forEach(function (item) {
collect(require('path').join(path, item), files);
});
} else if (path.match(/\.js$/)) {
files.push(path);
}
}
collect(_c.LIB, libs);
collect(_c.LIB + "../cli", libs);
collect(_c.LIB + "../server", libs);
collect(_c.ROOT + "test", tests);
libs.sort();
tests.sort();
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("| Lib | Lines | LOC |\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
libs.forEach(function (lib) {
parseFile(lib, function (lines, loc) {
lib_lines += lines;
lib_loc += loc;
});
});
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("| Total |");
process.stdout.write(" " + lib_lines + spaces(7 - String(lib_lines).length) + "|");
process.stdout.write(" " + lib_loc + spaces(7 - String(lib_loc).length) + "|\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("| Tests | Lines | LOC |\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
tests.forEach(function (test) {
parseFile(test, function (lines, loc) {
test_lines += lines;
test_loc += loc;
});
});
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("| Total |");
process.stdout.write(" " + test_lines + spaces(7 - String(test_lines).length) + "|");
process.stdout.write(" " + test_loc + spaces(7 - String(test_loc).length) + "|\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
total_lines = lib_lines + test_lines;
total_loc = lib_loc + test_loc;
testsOverLib = (lib_loc / test_loc).toFixed(2);
emptySpace = total_lines - total_loc;
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("| Stats |\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
process.stdout.write("| lines: " + total_lines + spaces(85 - String(total_lines).length) + "|\n");
process.stdout.write("| loc: " + total_loc + spaces(87 - String(total_loc).length) + "|\n");
process.stdout.write("| lib/test (loc): " + testsOverLib + spaces(76 - String(testsOverLib).length) + "|\n");
process.stdout.write("| comments & empty space: " + emptySpace + spaces(68 - String(emptySpace).length) + "|\n");
process.stdout.write("+---------------------------------------------------------------------------+--------+--------+\n");
};