tree.js
4.1 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const _ = require('lodash');
const filesize = require('filesize');
class Node {
constructor(name, parent) {
this.name = name;
this.parent = parent;
}
get path() {
const path = [];
let node = this;
while (node) {
path.push(node.name);
node = node.parent;
}
return path.reverse().join('/');
}
toString(indent) {
indent = indent || '|';
return `${indent} ${this.name}`;
}
}
class Module extends Node {
constructor(name, data, parent) {
super(name, parent);
this.data = data;
}
get size() {
return this.data.size;
}
get parsedSize() {
return this.data.parsedSize;
}
get gzipSize() {
return this.data.gzipSize;
}
mergeData(data) {
_.each(['size', 'parsedSize', 'gzipSize'], prop => {
if (data[prop]) {
this.data[prop] = (this.data[prop] || 0) + data[prop];
}
});
}
toString(indent) {
return `${super.toString(indent)} [${this.data.id}] (${filesize(this.size)})`;
}
toChartData() {
return {
id: this.data.id,
label: this.name,
path: this.path,
statSize: this.size,
parsedSize: this.parsedSize,
gzipSize: this.gzipSize
};
}
}
class Folder extends Node {
constructor(name, parent) {
super(name, parent);
this.children = Object.create(null);
}
get size() {
if (!_.has(this, '_size')) {
this._size = this.walk((node, size) => (size + node.size), 0);
}
return this._size;
}
get parsedSize() {
if (!_.has(this, '_parsedSize')) {
this._parsedSize = this.walk((node, size, stop) => {
if (node.parsedSize === undefined) {
return stop(undefined);
}
return (size + node.parsedSize);
}, 0);
}
return this._parsedSize;
}
get gzipSize() {
if (!_.has(this, '_gzipSize')) {
this._gzipSize = this.walk((node, size, stop) => {
if (node.gzipSize === undefined) {
return stop(undefined);
}
return (size + node.gzipSize);
}, 0);
}
return this._gzipSize;
}
getChild(name) {
return this.children[name];
}
addFolder(name) {
const folder = new Folder(name, this);
this.children[name] = folder;
delete this._size;
delete this._parsedSize;
return folder;
}
addModule(name, data) {
let node = this.children[name];
// For some reason we already have this node in children and it's a folder.
if (node && node instanceof Folder) return false;
if (node) {
// We already have this node in children and it's a module.
// Merging it's data.
node.mergeData(data);
} else {
// Creating new module.
node = new Module(name, data, this);
this.children[name] = node;
}
delete this._size;
delete this._parsedSize;
return true;
}
addModuleByPath(path, data) {
const [folderNames, fileName] = [path.slice(0, -1), _.last(path)];
let currentFolder = this;
_.each(folderNames, folderName => {
currentFolder = currentFolder.getChild(folderName) || currentFolder.addFolder(folderName);
});
currentFolder.addModule(fileName, data);
}
walk(walker, state = {}) {
let stopped = false;
_.each(this.children, child => {
if (child.walk) {
state = child.walk(walker, state, stop);
} else {
state = walker(child, state, stop);
}
if (stopped) return false;
});
return state;
function stop(finalState) {
stopped = true;
return finalState;
}
}
toString(indent, opts) {
const { sortBy } = opts || {};
indent = indent || '|';
let str = `${indent} ${this.name} (${filesize(this.size)})\n`;
str += _(this.children)
.sortBy(sortBy)
.reverse()
.map(child => child.toString(`${indent} |`, opts))
.join('\n');
return str;
}
toChartData() {
return {
label: this.name,
path: this.path,
statSize: this.size,
parsedSize: this.parsedSize,
gzipSize: this.gzipSize,
groups: _.invokeMap(this.children, 'toChartData')
};
}
}
module.exports = {
Node,
Module,
Folder
};