/
size.js
307 lines (249 loc) · 8.5 KB
/
size.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/**
Copyright 2017 Google Inc.
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
https://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.
*/
const glob = require('glob');
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const PluginInterface = require('./plugin-interface');
const POSITIVE_EMOJI = '✅';
const NEGATIVE_EMOJI = '🚫';
class SizePlugin extends PluginInterface {
constructor({globPattern, globOptions} = {}) {
super('PR-Bot Size Plugin');
this._globPattern = globPattern;
this._globOptions = globOptions;
}
run({beforePath, afterPath} = {}) {
if (!this._globPattern) {
throw new Error(`The '${this.name}' requires a ` +
`'globPattern' parameter in the constructor.`);
}
return Promise.all([
this._getFileDetails(beforePath),
this._getFileDetails(afterPath),
])
.then((results) => {
const beforeFiles = results[0];
const afterFiles = results[1];
const newFileList = [];
Object.keys(afterFiles).forEach((relativePathKey) => {
const fileDetails = afterFiles[relativePathKey];
const afterDetails = afterFiles[relativePathKey];
const beforeDetails = beforeFiles[relativePathKey];
// If there was a previous file but it's size was 0,
// treat it as a new file (avoids the divide by zero issue with)
// calculating sizeDiff
fileDetails.isNewFile = (typeof beforeDetails === 'undefined');
if (!fileDetails.isNewFile) {
fileDetails.previousSize = beforeDetails.sizeInBytes;
fileDetails.sizeDifferenceInBytes =
afterDetails.sizeInBytes - beforeDetails.sizeInBytes;
fileDetails.sizeDifferencePercent =
(afterDetails.sizeInBytes / beforeDetails.sizeInBytes) - 1;
} else {
fileDetails.previousSize = null;
fileDetails.sizeDifferenceInBytes = NaN;
fileDetails.sizeDifferencePercent = NaN;
}
newFileList.push(fileDetails);
});
return newFileList;
})
.then((allFileInfo) => {
return {
prettyLog: this.getPrettyLogResults(allFileInfo),
markdownLog: this.getMarkdownResults(allFileInfo),
};
});
}
_getFileDetails(directory) {
return new Promise((resolve, reject) => {
const globOptions = this._globOptions || {};
globOptions.absolute = true;
globOptions.cwd = directory;
globOptions.root = directory;
glob(this._globPattern, globOptions, (err, matches) => {
if (err) {
return reject(err);
}
resolve(matches);
});
})
.then((filePaths) => {
return filePaths.reduce((promiseChain, filePath) => {
return promiseChain.then((fileInfo) => {
return fs.stat(filePath)
.then((stats) => {
const relativePath = path.relative(directory, filePath);
fileInfo[relativePath] = {
relativePath,
sizeInBytes: stats.size
};
return fileInfo;
});
});
}, Promise.resolve({}));
});
}
static _convertSize(sizeInBytes) {
if (typeof sizeInBytes !== 'number') {
return null;
}
let fileSize = sizeInBytes;
let unit = 'B';
if (fileSize >= 1000) {
unit = 'KB';
fileSize = fileSize / 1000;
if (fileSize >= 1000) {
unit = 'MB';
fileSize = fileSize / 1000;
}
}
return {
size: fileSize,
unit,
};
}
getPrettyLogResults(allFileInfo) {
let changedFileInfo = allFileInfo.filter((fileInfo) => {
return fileInfo.isNewFile === false && (fileInfo.sizeDifferenceInBytes !== 0);
});
const changedFileRows = changedFileInfo.map((fileInfo) => {
const newSizeDetails = SizePlugin._convertSize(fileInfo.sizeInBytes);
const prevSizeDetails = SizePlugin._convertSize(fileInfo.previousSize);
let percentChangeColor = chalk.dim;
if (fileInfo.sizeDifferencePercent >= 0.1) {
percentChangeColor = chalk.red;
} else if (fileInfo.sizeDifferencePercent <= -0.1) {
percentChangeColor = chalk.green;
}
let prettyFloat = parseFloat(fileInfo.sizeDifferencePercent * 100)
.toFixed(2);
let prefix = '';
if (fileInfo.sizeDifferencePercent > 0) {
prefix = '+';
}
let percentString = percentChangeColor(`${prefix}${prettyFloat}%`);
return [
chalk.yellow(fileInfo.relativePath),
chalk.dim(`${prevSizeDetails.size} ${prevSizeDetails.unit}`),
chalk.dim(`>`),
chalk.blue(`${newSizeDetails.size} ${newSizeDetails.unit}`),
percentString
];
});
let newFileInfo = allFileInfo.filter((fileInfo) => {
return fileInfo.isNewFile;
});
let newFileRows = newFileInfo.map((fileInfo) => {
const newSizeDetails = SizePlugin._convertSize(fileInfo.sizeInBytes);
return [
chalk.yellow(fileInfo.relativePath),
chalk.blue(`${newSizeDetails.size} ${newSizeDetails.unit}`),
];
});
let changedTable = this.createLogTable(changedFileRows);
let newTable = this.createLogTable(newFileRows);
if (!changedTable) {
changedTable = 'No file sizes have changed.';
}
if (!newTable) {
newTable = 'No new files have been added.';
}
const changedTitle = 'Changed File Sizes';
const changedTitleBar = '-'.repeat(changedTitle.length)
const allChangedTable = [changedTitle, changedTitleBar, changedTable]
.join('\n');
const newTitle = 'New Files';
const newTitleBar = '-'.repeat(newTitle.length)
const allNewTable = [newTitle, newTitleBar, newTable]
.join('\n');
return '\n' + [allChangedTable, allNewTable].join('\n\n') + '\n';
}
getMarkdownResults(allFileInfo) {
let changedHeadings = [
'File',
'Before',
'After',
'Change',
''
];
let changedFileInfo = allFileInfo.filter((fileInfo) => {
return fileInfo.isNewFile === false && (fileInfo.sizeDifferenceInBytes !== 0);
});
const changedFileRows = this._getMDFileRows(changedFileInfo);
const fullFileRows = this._getMDFileRows(allFileInfo);
let newHeadings = [
'File',
'Size',
];
let newFileInfo = allFileInfo.filter((fileInfo) => {
return fileInfo.isNewFile;
});
let newFileRows = newFileInfo.map((fileInfo) => {
const newSizeDetails = SizePlugin._convertSize(fileInfo.sizeInBytes);
return [
fileInfo.relativePath,
`${newSizeDetails.size} ${newSizeDetails.unit}`,
];
});
let changedTable = this.createMDTable(changedHeadings, changedFileRows);
let newTable = this.createMDTable(newHeadings, newFileRows);
let fullTable = this.createMDTable(changedHeadings, fullFileRows);
if (!changedTable) {
changedTable = 'No file sizes have changed.';
}
if (!newTable) {
newTable = 'No new files have been added.';
}
return `#### Changed File Sizes
${changedTable}
#### New Files
${newTable}
#### All File Sizes
<details>
<summary>View Table</summary>
${fullTable}
</details>`;
}
_getMDFileRows(fileDetails) {
return fileDetails.map((fileInfo) => {
const newSizeDetails = SizePlugin._convertSize(fileInfo.sizeInBytes);
const prevSizeDetails = SizePlugin._convertSize(fileInfo.previousSize);
let percentString = '';
if (!isNaN(fileInfo.sizeDifferencePercent)) {
let prettyFloat = parseFloat(fileInfo.sizeDifferencePercent * 100)
.toFixed(2);
let prefix = '';
if (fileInfo.sizeDifferencePercent > 0) {
prefix = '+';
}
percentString = `${prefix}${prettyFloat}%`;
}
let emoji = '';
if (fileInfo.sizeDifferencePercent > 0.1) {
emoji = '☠️';
} else if(fileInfo.sizeDifferencePercent < -0.1) {
emoji = '🎉';
}
return [
fileInfo.relativePath,
prevSizeDetails ? `${prevSizeDetails.size} ${prevSizeDetails.unit}` : '',
`${newSizeDetails.size} ${newSizeDetails.unit}`,
percentString,
emoji
];
});
}
}
module.exports = SizePlugin;