1- import fs from 'fs'
21import { Context } from '../types.js'
32import { chromium , firefox , webkit , Browser } from '@playwright/test'
43import constants from './constants.js' ;
54import chalk from 'chalk' ;
65import axios from 'axios' ;
6+ import fs from 'fs/promises' ;
77
88import { globalAgent } from 'http' ;
99import { promisify } from 'util'
@@ -229,17 +229,10 @@ export function getRenderViewportsForOptions(options: any): Array<Record<string,
229229
230230// Background polling function
231231export async function startPolling ( ctx : Context , build_id : string , baseline : boolean , projectToken : string ) : Promise < void > {
232- let filename = '' ;
233232 let isPollingActive = true ;
234233 if ( build_id ) {
235- filename = `${ build_id } .json`
236234 ctx . log . info ( `Fetching results for buildId ${ build_id } in progress....` ) ;
237235 } else if ( ctx . build && ctx . build . id ) {
238- if ( ctx . options . fetchResultsFileName ) {
239- filename = ctx . options . fetchResultsFileName
240- } else {
241- filename = `${ ctx . build . id } .json`
242- }
243236 ctx . log . info ( `Fetching results for buildId ${ ctx . build . id } in progress....` ) ;
244237 }
245238
@@ -262,16 +255,19 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
262255 if ( ! resp . build ) {
263256 ctx . log . info ( "Error: Build data is null." ) ;
264257 clearInterval ( intervalId ) ;
265- // isPollingActive = false ;
258+ return ;
266259 }
267260
268- fs . writeFileSync ( filename , JSON . stringify ( resp , null , 2 ) ) ;
269- ctx . log . debug ( `Updated results in ${ filename } ` ) ;
261+ let fileName = `${ resp . build . build_id } .json`
262+ if ( ctx . options . fetchResults && ctx . options . fetchResultsFileName && ctx . build && ctx . build . id && resp . build . build_id === ctx . build . id ) {
263+ fileName = `${ ctx . options . fetchResultsFileName } `
264+ }
265+ await fs . writeFile ( `${ fileName } ` , JSON . stringify ( resp , null , 2 ) ) ;
266+ ctx . log . debug ( `Updated results in ${ fileName } ` ) ;
270267
271268 if ( resp . build . build_status_ind === constants . BUILD_COMPLETE || resp . build . build_status_ind === constants . BUILD_ERROR ) {
272269 clearInterval ( intervalId ) ;
273- ctx . log . info ( `Fetching results completed. Final results written to ${ filename } ` ) ;
274- // isPollingActive = false;
270+ ctx . log . info ( `Fetching results completed. Final results written to ${ fileName } ` ) ;
275271
276272
277273 // Evaluating Summary
@@ -309,7 +305,7 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
309305 `${ chalk . yellow ( 'Project Name:' ) } ${ chalk . white ( resp . project . name ) } \n` +
310306 `${ chalk . yellow ( 'Build ID:' ) } ${ chalk . white ( resp . build . build_id ) } \n`
311307 )
312- ) ;
308+ ) ;
313309 }
314310 } catch ( error : any ) {
315311 if ( error . message . includes ( 'ENOTFOUND' ) ) {
@@ -319,7 +315,6 @@ export async function startPolling(ctx: Context, build_id: string, baseline: boo
319315 ctx . log . error ( `Error fetching screenshot data: ${ error . message } ` ) ;
320316 }
321317 clearInterval ( intervalId ) ;
322- // isPollingActive = false;
323318 }
324319 } , 5000 ) ;
325320}
0 commit comments