@@ -25,7 +25,7 @@ const datastore = require('./datastore');
2525const { createOrUpdateComment} = require ( './make-comment' ) ;
2626
2727/**
28- * Generates and submits a comment. If this is run on main branch, data is
28+ * Generates and submits a comment. If this is run on the main or release branch, data is
2929 * committed to the store instead.
3030 * @param {{
3131 'android-hermes-arm64-v8a'?: number;
@@ -47,8 +47,7 @@ async function reportSizeStats(stats, replacePattern) {
4747 ) ;
4848 const collection = datastore . getBinarySizesCollection ( store ) ;
4949
50- // Collect the current sizes for main branch only.
51- if ( GITHUB_REF === 'main' ) {
50+ if ( GITHUB_REF === 'main' || GITHUB_REF . endsWith ( '-stable' ) ) {
5251 // Ensure we only store numbers greater than zero.
5352 const validatedStats = Object . keys ( stats ) . reduce ( ( validated , key ) => {
5453 const value = stats [ key ] ;
@@ -59,65 +58,85 @@ async function reportSizeStats(stats, replacePattern) {
5958 validated [ key ] = value ;
6059 return validated ;
6160 } , { } ) ;
61+
6262 if ( Object . keys ( validatedStats ) . length > 0 ) {
63+ // Print out the new stats
64+ const document =
65+ ( await datastore . getLatestDocument ( collection , GITHUB_REF ) ) || { } ;
66+ const formattedStats = formatBundleStats ( document , validatedStats ) ;
67+ console . log ( formattedStats ) ;
68+
6369 await datastore . createOrUpdateDocument (
6470 collection ,
6571 GITHUB_SHA ,
6672 validatedStats ,
73+ GITHUB_REF ,
6774 ) ;
6875 }
69- } else if ( GITHUB_REF . endsWith ( '-stable' ) ) {
70- console . log ( `Skipping bundle size reporting for branch: ${ GITHUB_REF } ` ) ;
7176 } else {
72- const document = await datastore . getLatestDocument ( collection ) ;
73-
74- const diffFormatter = new Intl . NumberFormat ( 'en' , { signDisplay : 'always' } ) ;
75- const sizeFormatter = new Intl . NumberFormat ( 'en' , { } ) ;
76-
77- // | Platform | Engine | Arch | Size (bytes) | Diff |
78- // |:---------|:-------|:------------|-------------:|-----:|
79- // | android | hermes | arm64-v8a | 9437184 | ±0 |
80- // | android | hermes | armeabi-v7a | 9015296 | ±0 |
81- // | android | hermes | x86 | 9498624 | ±0 |
82- // | android | hermes | x86_64 | 9965568 | ±0 |
83- // | android | jsc | arm64-v8a | 9236480 | ±0 |
84- // | android | jsc | armeabi-v7a | 8814592 | ±0 |
85- // | android | jsc | x86 | 9297920 | ±0 |
86- // | android | jsc | x86_64 | 9764864 | ±0 |
87- // | android | jsc | x86_64 | 9764864 | ±0 |
88- // | ios | - | universal | 10715136 | ±0 |
89- const comment = [
90- '| Platform | Engine | Arch | Size (bytes) | Diff |' ,
91- '|:---------|:-------|:-----|-------------:|-----:|' ,
92- ...Object . keys ( stats ) . map ( identifier => {
93- const [ size , diff ] = ( ( ) => {
94- const statSize = stats [ identifier ] ;
95- if ( ! statSize ) {
96- return [ 'n/a' , '--' ] ;
97- } else if ( ! ( identifier in document ) ) {
98- return [ statSize , 'n/a' ] ;
99- } else {
100- return [
101- sizeFormatter . format ( statSize ) ,
102- diffFormatter . format ( statSize - document [ identifier ] ) ,
103- ] ;
104- }
105- } ) ( ) ;
106-
107- const [ platform , engineOrArch , ...archParts ] = identifier . split ( '-' ) ;
108- const arch = archParts . join ( '-' ) || engineOrArch ;
109- const engine = arch === engineOrArch ? '-' : engineOrArch ; // e.g. 'ios-universal'
110- return `| ${ platform } | ${ engine } | ${ arch } | ${ size } | ${ diff } |` ;
111- } ) ,
112- '' ,
113- `Base commit: ${ document . commit } ` ,
114- ] . join ( '\n' ) ;
77+ // For PRs, always compare vs main.
78+ const document =
79+ ( await datastore . getLatestDocument ( collection , 'main' ) ) || { } ;
80+ const comment = formatBundleStats ( document , stats ) ;
11581 createOrUpdateComment ( comment , replacePattern ) ;
11682 }
11783
11884 await datastore . terminateStore ( store ) ;
11985}
12086
87+ /**
88+ * Format the new bundle stats as compared to the latest stored entry.
89+ * @param {firebase.firestore.DocumentData } document the latest entry to compare against
90+ * @param {firebase.firestore.UpdateData } stats The stats to be formatted
91+ * @returns {string }
92+ */
93+ function formatBundleStats ( document , stats ) {
94+ const diffFormatter = new Intl . NumberFormat ( 'en' , { signDisplay : 'always' } ) ;
95+ const sizeFormatter = new Intl . NumberFormat ( 'en' , { } ) ;
96+
97+ // | Platform | Engine | Arch | Size (bytes) | Diff |
98+ // |:---------|:-------|:------------|-------------:|-----:|
99+ // | android | hermes | arm64-v8a | 9437184 | ±0 |
100+ // | android | hermes | armeabi-v7a | 9015296 | ±0 |
101+ // | android | hermes | x86 | 9498624 | ±0 |
102+ // | android | hermes | x86_64 | 9965568 | ±0 |
103+ // | android | jsc | arm64-v8a | 9236480 | ±0 |
104+ // | android | jsc | armeabi-v7a | 8814592 | ±0 |
105+ // | android | jsc | x86 | 9297920 | ±0 |
106+ // | android | jsc | x86_64 | 9764864 | ±0 |
107+ // | android | jsc | x86_64 | 9764864 | ±0 |
108+ // | ios | - | universal | 10715136 | ±0 |
109+ const formatted = [
110+ '| Platform | Engine | Arch | Size (bytes) | Diff |' ,
111+ '|:---------|:-------|:-----|-------------:|-----:|' ,
112+ ...Object . keys ( stats ) . map ( identifier => {
113+ const [ size , diff ] = ( ( ) => {
114+ const statSize = stats [ identifier ] ;
115+ if ( ! statSize ) {
116+ return [ 'n/a' , '--' ] ;
117+ } else if ( ! ( identifier in document ) ) {
118+ return [ statSize , 'n/a' ] ;
119+ } else {
120+ return [
121+ sizeFormatter . format ( statSize ) ,
122+ diffFormatter . format ( statSize - document [ identifier ] ) ,
123+ ] ;
124+ }
125+ } ) ( ) ;
126+
127+ const [ platform , engineOrArch , ...archParts ] = identifier . split ( '-' ) ;
128+ const arch = archParts . join ( '-' ) || engineOrArch ;
129+ const engine = arch === engineOrArch ? '-' : engineOrArch ; // e.g. 'ios-universal'
130+ return `| ${ platform } | ${ engine } | ${ arch } | ${ size } | ${ diff } |` ;
131+ } ) ,
132+ '' ,
133+ `Base commit: ${ document . commit || '<unknown>' } ` ,
134+ `Branch: ${ document . branch || '<unknown>' } ` ,
135+ ] . join ( '\n' ) ;
136+
137+ return formatted ;
138+ }
139+
121140/**
122141 * Returns the size of the file at specified path in bytes.
123142 * @param {fs.PathLike } path
0 commit comments