/
report-bundle-size.js
258 lines (232 loc) · 7.61 KB
/
report-bundle-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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const {
GITHUB_TOKEN,
GITHUB_OWNER,
GITHUB_REPO,
GITHUB_PR_NUMBER,
GITHUB_REF,
GITHUB_SHA,
} = process.env;
const datastore = require('./datastore');
const {
createOrUpdateComment,
validateEnvironment: validateEnvironmentForMakeComment,
} = require('./make-comment');
const fs = require('fs');
/**
* Generates and submits a comment. If this is run on the main or release branch, data is
* committed to the store instead.
* @param {{
'android-hermes-arm64-v8a'?: number;
'android-hermes-armeabi-v7a'?: number;
'android-hermes-x86'?: number;
'android-hermes-x86_64'?: number;
'android-jsc-arm64-v8a'?: number;
'android-jsc-armeabi-v7a'?: number;
'android-jsc-x86'?: number;
'android-jsc-x86_64'?: number;
'ios-universal'?: number;
}} stats
*/
async function reportSizeStats(stats, replacePattern) {
const {FIREBASE_APP_EMAIL, FIREBASE_APP_PASS} = process.env;
const store = await datastore.initializeStore(
FIREBASE_APP_EMAIL,
FIREBASE_APP_PASS,
);
const collection = datastore.getBinarySizesCollection(store);
if (!isPullRequest(GITHUB_REF)) {
// Ensure we only store numbers greater than zero.
const validatedStats = Object.keys(stats).reduce((validated, key) => {
const value = stats[key];
if (typeof value !== 'number' || value <= 0) {
return validated;
}
validated[key] = value;
return validated;
}, {});
if (Object.keys(validatedStats).length > 0) {
// Print out the new stats
const document =
(await datastore.getLatestDocument(collection, GITHUB_REF)) || {};
const formattedStats = formatBundleStats(document, validatedStats);
console.log(formattedStats);
await datastore.createOrUpdateDocument(
collection,
GITHUB_SHA,
validatedStats,
GITHUB_REF,
);
}
} else {
const params = {
auth: GITHUB_TOKEN,
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};
// For PRs, always compare vs main.
const document =
(await datastore.getLatestDocument(collection, 'main')) || {};
const comment = formatBundleStats(document, stats);
createOrUpdateComment(params, comment, replacePattern);
}
await datastore.terminateStore(store);
}
/**
* Format the new bundle stats as compared to the latest stored entry.
* @param {firebase.firestore.DocumentData} document the latest entry to compare against
* @param {firebase.firestore.UpdateData} stats The stats to be formatted
* @returns {string}
*/
function formatBundleStats(document, stats) {
const diffFormatter = new Intl.NumberFormat('en', {signDisplay: 'always'});
const sizeFormatter = new Intl.NumberFormat('en', {});
// | Platform | Engine | Arch | Size (bytes) | Diff |
// |:---------|:-------|:------------|-------------:|-----:|
// | android | hermes | arm64-v8a | 9437184 | ±0 |
// | android | hermes | armeabi-v7a | 9015296 | ±0 |
// | android | hermes | x86 | 9498624 | ±0 |
// | android | hermes | x86_64 | 9965568 | ±0 |
// | android | jsc | arm64-v8a | 9236480 | ±0 |
// | android | jsc | armeabi-v7a | 8814592 | ±0 |
// | android | jsc | x86 | 9297920 | ±0 |
// | android | jsc | x86_64 | 9764864 | ±0 |
// | android | jsc | x86_64 | 9764864 | ±0 |
// | ios | - | universal | 10715136 | ±0 |
const formatted = [
'| Platform | Engine | Arch | Size (bytes) | Diff |',
'|:---------|:-------|:-----|-------------:|-----:|',
...Object.keys(stats).map(identifier => {
const [size, diff] = (() => {
const statSize = stats[identifier];
if (!statSize) {
return ['n/a', '--'];
} else if (!(identifier in document)) {
return [statSize, 'n/a'];
} else {
return [
sizeFormatter.format(statSize),
diffFormatter.format(statSize - document[identifier]),
];
}
})();
const [platform, engineOrArch, ...archParts] = identifier.split('-');
const arch = archParts.join('-') || engineOrArch;
const engine = arch === engineOrArch ? '-' : engineOrArch; // e.g. 'ios-universal'
return `| ${platform} | ${engine} | ${arch} | ${size} | ${diff} |`;
}),
'',
`Base commit: ${document.commit || '<unknown>'}`,
`Branch: ${document.branch || '<unknown>'}`,
].join('\n');
return formatted;
}
/**
* Returns the size of the file at specified path in bytes.
* @param {fs.PathLike} path
* @returns {number}
*/
function getFileSize(path) {
try {
const stats = fs.statSync(path);
return stats.size;
} catch {
return 0;
}
}
/**
* Returns the size of the APK for specified JS engine and architecture.
* @param {'hermes' | 'jsc'} engine
* @param {'arm64-v8a' | 'armeabi-v7a' | 'x86' | 'x86_64'} arch
*/
function android_getApkSize(engine, arch) {
return getFileSize(
`packages/rn-tester/android/app/build/outputs/apk/${engine}/release/app-${engine}-${arch}-release.apk`,
);
}
/**
* Returns whether the specified ref points to a pull request.
*/
function isPullRequest(ref) {
return ref !== 'main' && !/^\d+\.\d+-stable$/.test(ref);
}
/**
* Validates that required environment variables are set.
* @returns {boolean} `true` if everything is in order; `false` otherwise.
*/
function validateEnvironment() {
if (!GITHUB_REF) {
console.error("Missing GITHUB_REF. This should've been set by the CI.");
return false;
}
if (isPullRequest(GITHUB_REF)) {
if (!validateEnvironmentForMakeComment()) {
return false;
}
} else if (!GITHUB_SHA) {
// To update the data store, we need the SHA associated with the build
console.error("Missing GITHUB_SHA. This should've been set by the CI.");
return false;
}
console.log(` GITHUB_SHA=${GITHUB_SHA}`);
return true;
}
/**
* Reports app bundle size.
* @param {string} target
*/
async function report(target) {
switch (target) {
case 'android':
await reportSizeStats(
{
'android-hermes-arm64-v8a': android_getApkSize('hermes', 'arm64-v8a'),
'android-hermes-armeabi-v7a': android_getApkSize(
'hermes',
'armeabi-v7a',
),
'android-hermes-x86': android_getApkSize('hermes', 'x86'),
'android-hermes-x86_64': android_getApkSize('hermes', 'x86_64'),
'android-jsc-arm64-v8a': android_getApkSize('jsc', 'arm64-v8a'),
'android-jsc-armeabi-v7a': android_getApkSize('jsc', 'armeabi-v7a'),
'android-jsc-x86': android_getApkSize('jsc', 'x86'),
'android-jsc-x86_64': android_getApkSize('jsc', 'x86_64'),
},
'\\| android \\| hermes \\| arm',
);
break;
case 'ios':
await reportSizeStats(
{
'ios-universal': getFileSize(
'packages/rn-tester/build/Build/Products/Release-iphonesimulator/RNTester.app/RNTester',
),
},
'\\| ios \\| - \\| universal \\|',
);
break;
default: {
const path = require('path');
console.log(`Syntax: ${path.basename(process.argv[1])} [android | ios]`);
process.exitCode = 2;
break;
}
}
}
if (!validateEnvironment()) {
process.exit(1);
}
const {[2]: target} = process.argv;
report(target).catch(error => {
console.error(error);
process.exitCode = 1;
});