-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathprintCommitStats.mjs
More file actions
154 lines (127 loc) · 4.27 KB
/
Copy pathprintCommitStats.mjs
File metadata and controls
154 lines (127 loc) · 4.27 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* eslint-disable no-console */
/* eslint-disable no-magic-numbers */
/* eslint-env node */
import chalk from 'chalk';
import { exec } from 'child_process';
import { parsePatch } from 'diff';
import { minimatch } from 'minimatch';
import { promisify } from 'util';
function getCategory(
/** @type { string } */
path
) {
return minimatch(path, '*/packages/test/**')
? 'test'
: minimatch(path, '**/*.spec.*')
? 'test'
: minimatch(path, '**/*.test.*')
? 'test'
: minimatch(path, '*/packages/**/src/**/*')
? 'production'
: minimatch(path, '**/*.md')
? 'doc'
: minimatch(path, '*/__tests__/**/*')
? 'test'
: minimatch(path, '**/package-lock.json')
? 'generated'
: 'others';
}
function toIntegerOrFixed(
/** @type { number } */
value,
/** @type { number } */
fractionDigits = 2
) {
return value % 1 === 0 ? value : value.toFixed(fractionDigits);
}
function toRatio(
/** @type { number } */
x,
/** @type { number } */
y
) {
if (x > y) {
if (y === 0) {
return [x, 0];
}
return [x / y, 1];
}
if (x === 0) {
return [0, y];
}
return [1, y / x];
}
function toRatioString(
/** @type { number } */
x,
/** @type { number } */
y,
positiveIsGood = false
) {
const chalks = x === y ? [chalk.yellow, chalk.yellow] : [chalk.green, chalk.red];
const [ratio1, ratio2] = toRatio(x, y);
positiveIsGood || chalks.reverse();
return chalks[x > y ? 0 : 1](`${toIntegerOrFixed(ratio1)}:${toIntegerOrFixed(ratio2)}`);
}
(async () => {
const { stdout: diffContent } = await promisify(exec)('git diff origin/main', {
encoding: 'utf-8',
maxBuffer: 50 * 1024 * 1024
});
const patches = parsePatch(diffContent);
/** @type { Map<'doc' | 'generated' | 'others' | 'production' | 'test', { numFile: number; numLineAdded: number; numLineRemoved: number; }> } */
const stats = new Map();
stats.set('doc', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('generated', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('others', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('production', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
stats.set('test', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 });
patches.forEach(patch => {
if (patch.newFileName || patch.oldFileName) {
const stat = stats.get(getCategory(patch.newFileName || patch.oldFileName));
stat.numFile++;
for (const hunk of patch.hunks) {
for (const line of hunk.lines) {
if (line.startsWith('+') && !line.startsWith('+++') && line.substring(1).trim()) {
stat.numLineAdded++;
}
if (line.startsWith('-') && !line.startsWith('---') && line.substring(1).trim()) {
stat.numLineRemoved++;
}
}
}
}
});
console.log(
'> Disclaimer: *do not use* any results here to quantify how good this commit. Numbers should never be used to judge quality work and passionate people.'
);
console.log();
console.log('| | Files | Lines added | Lines added and removed | Lines ratio |');
console.log('| - | - | - | - | - |');
for (const [name, type] of [
['Production code', 'production'],
['Test code', 'test'],
['Documentation', 'doc'],
['Generated code', 'generated'],
['Others', 'others']
]) {
console.log(
`| ${[
name,
`${stats.get(type).numFile} files`,
`${stats.get(type).numLineAdded} lines added`,
`${stats.get(type).numLineAdded + stats.get(type).numLineRemoved} lines total`,
`${toRatioString(stats.get(type).numLineAdded, stats.get(type).numLineRemoved)}`
].join(' | ')} |`
);
}
console.log();
const [prodRatio, testRatio] = toRatio(stats.get('production').numLineAdded, stats.get('test').numLineAdded);
console.log(
`Test to code ratio = ${toRatioString(stats.get('test').numLineAdded, stats.get('production').numLineAdded)}`
);
console.log();
console.log(
`${prodRatio === 0 || testRatio > prodRatio ? '😇' : '🚨'} There are ${chalk.magenta(toIntegerOrFixed(testRatio))} lines of test code added for every ${chalk.magenta(toIntegerOrFixed(prodRatio))} lines of production code added.`
);
})();