@@ -8,6 +8,7 @@ import axios from 'axios';
88import { globalAgent } from 'http' ;
99import { promisify } from 'util'
1010const sleep = promisify ( setTimeout ) ;
11+ import { build } from 'tsup' ;
1112
1213let isPollingActive = false ;
1314let globalContext : Context ;
@@ -227,9 +228,12 @@ process.on('SIGINT', async () => {
227228} ) ;
228229
229230// Background polling function
230- export async function startPolling ( ctx : Context ) : Promise < void > {
231- ctx . log . info ( 'Fetching results in progress....' ) ;
232- ctx . log . debug ( ctx . build ) ;
231+ export async function startPolling ( ctx : Context , build_id : string , baseline : boolean , projectToken : string ) : Promise < void > {
232+ if ( build_id ) {
233+ ctx . log . info ( `Fetching results for buildId ${ build_id } in progress....` ) ;
234+ } else if ( ctx . build && ctx . build . id ) {
235+ ctx . log . info ( `Fetching results for buildId ${ ctx . build . id } in progress....` ) ;
236+ }
233237 isPollingActive = true ;
234238
235239 const intervalId = setInterval ( async ( ) => {
@@ -239,21 +243,35 @@ export async function startPolling(ctx: Context): Promise<void> {
239243 }
240244
241245 try {
242- const resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline || false , ctx . log ) ;
246+ let resp ;
247+ let filename ;
248+ if ( build_id ) {
249+ resp = await ctx . client . getScreenshotData ( build_id , baseline , ctx . log , projectToken ) ;
250+ filename = `${ build_id } .json`
251+ } else if ( ctx . build && ctx . build . id ) {
252+ resp = await ctx . client . getScreenshotData ( ctx . build . id , ctx . build . baseline , ctx . log , '' ) ;
253+ if ( ctx . options . fetchResultsFileName ) {
254+ filename = ctx . options . fetchResultsFileName
255+ } else {
256+ filename = `${ ctx . build . id } .json`
257+ }
258+ } else {
259+ return ;
260+ }
243261
244262 if ( ! resp . build ) {
245263 ctx . log . info ( "Error: Build data is null." ) ;
246264 clearInterval ( intervalId ) ;
247- isPollingActive = false ;
265+ // isPollingActive = false;
248266 }
249267
250- fs . writeFileSync ( ctx . options . fetchResultsFileName , JSON . stringify ( resp , null , 2 ) ) ;
251- ctx . log . debug ( `Updated results in ${ ctx . options . fetchResultsFileName } ` ) ;
268+ fs . writeFileSync ( filename , JSON . stringify ( resp , null , 2 ) ) ;
269+ ctx . log . debug ( `Updated results in ${ filename } ` ) ;
252270
253271 if ( resp . build . build_status_ind === constants . BUILD_COMPLETE || resp . build . build_status_ind === constants . BUILD_ERROR ) {
254272 clearInterval ( intervalId ) ;
255- ctx . log . info ( `Fetching results completed. Final results written to ${ ctx . options . fetchResultsFileName } ` ) ;
256- isPollingActive = false ;
273+ ctx . log . info ( `Fetching results completed. Final results written to ${ filename } ` ) ;
274+ // isPollingActive = false;
257275
258276
259277 // Evaluating Summary
@@ -284,7 +302,7 @@ export async function startPolling(ctx: Context): Promise<void> {
284302 // Display summary
285303 ctx . log . info (
286304 chalk . green . bold (
287- `\nSummary of Mismatches: \n` +
305+ `\nSummary of Mismatches for buildId: ${ build_id } \n` +
288306 `${ chalk . yellow ( 'Total Variants with Mismatches:' ) } ${ chalk . white ( totalVariantsWithMismatches ) } out of ${ chalk . white ( totalVariants ) } \n` +
289307 `${ chalk . yellow ( 'Total Screenshots with Mismatches:' ) } ${ chalk . white ( totalScreenshotsWithMismatches ) } out of ${ chalk . white ( totalScreenshots ) } \n` +
290308 `${ chalk . yellow ( 'Branch Name:' ) } ${ chalk . white ( resp . build . branch ) } \n` +
@@ -301,7 +319,7 @@ export async function startPolling(ctx: Context): Promise<void> {
301319 ctx . log . error ( `Error fetching screenshot data: ${ error . message } ` ) ;
302320 }
303321 clearInterval ( intervalId ) ;
304- isPollingActive = false ;
322+ // isPollingActive = false;
305323 }
306324 } , 5000 ) ;
307325}
0 commit comments