Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/stats.ts
4770 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import es from 'event-stream';
7
import fancyLog from 'fancy-log';
8
import ansiColors from 'ansi-colors';
9
import File from 'vinyl';
10
11
class Entry {
12
readonly name: string;
13
public totalCount: number;
14
public totalSize: number;
15
16
constructor(name: string, totalCount: number, totalSize: number) {
17
this.name = name;
18
this.totalCount = totalCount;
19
this.totalSize = totalSize;
20
}
21
22
toString(pretty?: boolean): string {
23
if (!pretty) {
24
if (this.totalCount === 1) {
25
return `${this.name}: ${this.totalSize} bytes`;
26
} else {
27
return `${this.name}: ${this.totalCount} files with ${this.totalSize} bytes`;
28
}
29
} else {
30
if (this.totalCount === 1) {
31
return `Stats for '${ansiColors.grey(this.name)}': ${Math.round(this.totalSize / 1204)}KB`;
32
33
} else {
34
const count = this.totalCount < 100
35
? ansiColors.green(this.totalCount.toString())
36
: ansiColors.red(this.totalCount.toString());
37
38
return `Stats for '${ansiColors.grey(this.name)}': ${count} files, ${Math.round(this.totalSize / 1204)}KB`;
39
}
40
}
41
}
42
}
43
44
const _entries = new Map<string, Entry>();
45
46
export function createStatsStream(group: string, log?: boolean): es.ThroughStream {
47
48
const entry = new Entry(group, 0, 0);
49
_entries.set(entry.name, entry);
50
51
return es.through(function (data) {
52
const file = data as File;
53
if (typeof file.path === 'string') {
54
entry.totalCount += 1;
55
if (Buffer.isBuffer(file.contents)) {
56
entry.totalSize += file.contents.length;
57
} else if (file.stat && typeof file.stat.size === 'number') {
58
entry.totalSize += file.stat.size;
59
} else {
60
// funky file...
61
}
62
}
63
this.emit('data', data);
64
}, function () {
65
if (log) {
66
if (entry.totalCount === 1) {
67
fancyLog(`Stats for '${ansiColors.grey(entry.name)}': ${Math.round(entry.totalSize / 1204)}KB`);
68
69
} else {
70
const count = entry.totalCount < 100
71
? ansiColors.green(entry.totalCount.toString())
72
: ansiColors.red(entry.totalCount.toString());
73
74
fancyLog(`Stats for '${ansiColors.grey(entry.name)}': ${count} files, ${Math.round(entry.totalSize / 1204)}KB`);
75
}
76
}
77
78
this.emit('end');
79
});
80
}
81
82