| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Imports
 | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2017-11-18 12:26:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Get branch and project name from command line arguments.
 | 
					
						
							|  |  |  | const [, , limitFile, project, branch, commit] = process.argv; | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Load sizes.
 | 
					
						
							|  |  |  | const currentSizes = JSON.parse(fs.readFileSync('/tmp/current.log', 'utf8')); | 
					
						
							|  |  |  | const allLimitSizes = JSON.parse(fs.readFileSync(limitFile, 'utf8')); | 
					
						
							|  |  |  | const limitSizes = allLimitSizes[project][branch] || allLimitSizes[project]['master']; | 
					
						
							| 
									
										
										
										
											2017-11-18 12:26:33 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | // Check current sizes against limits.
 | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | let failed = false; | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  | for (const compressionType in limitSizes) { | 
					
						
							|  |  |  |   if (typeof limitSizes[compressionType] === 'object') { | 
					
						
							|  |  |  |     const limitPerFile = limitSizes[compressionType]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (const filename in limitPerFile) { | 
					
						
							|  |  |  |       const expectedSize = limitPerFile[filename]; | 
					
						
							|  |  |  |       const actualSize = currentSizes[`${compressionType}/${filename}`]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (Math.abs(actualSize - expectedSize) > expectedSize / 100) { | 
					
						
							| 
									
										
										
										
											2017-11-18 12:26:33 -08:00
										 |  |  |         failed = true; | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  |         console.log( | 
					
						
							|  |  |  |             `Commit ${commit} ${compressionType} ${filename} exceeded expected size by >1% ` + | 
					
						
							| 
									
										
										
										
											2017-12-04 16:28:06 -08:00
										 |  |  |             `(expected: ${expectedSize}, actual: ${actualSize}).`); | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (failed) { | 
					
						
							| 
									
										
										
										
											2017-12-04 16:28:06 -08:00
										 |  |  |   console.log(`If this is a desired change, please update the size limits in file '${limitFile}'.`); | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  |   process.exit(1); | 
					
						
							|  |  |  | } else { | 
					
						
							| 
									
										
										
										
											2017-11-29 12:47:04 +02:00
										 |  |  |   console.log('Payload size <1% change check passed.'); | 
					
						
							| 
									
										
										
										
											2017-11-03 15:20:09 -07:00
										 |  |  | } |