@@ -98,35 +98,31 @@ const downloadAndUnzip = async (filePath, fileName, url) => {
9898 const writer = fs . createWriteStream ( tmpFilePath ) ;
9999
100100 return new Promise ( async ( resolve , reject ) => {
101- try {
102- request . get ( url ) . on ( 'response' , function ( response ) {
103-
104- if ( response . statusCode != 200 ) {
105- reject ( ) ;
106- } else {
107- //ensure that the user can call `then()` only when the file has
108- //been downloaded entirely.
109- response . pipe ( writer ) ;
110- let error = null ;
111- writer . on ( 'error' , err => {
112- error = err ;
113- writer . close ( ) ;
114- reject ( err ) ;
115- } ) ;
116- writer . on ( 'close' , async ( ) => {
117- if ( ! error ) {
118- await unzipFile ( filePath , fileName ) ;
119- fs . unlinkSync ( tmpFilePath ) ;
120- resolve ( true ) ;
121- }
122- //no need to call the reject here, as it will have been called in the
123- //'error' stream;
124- } ) ;
125- }
126- } ) ;
127- } catch ( err ) {
128- reject ( err ) ;
129- }
101+ request . get ( url ) . on ( 'response' , function ( response ) {
102+
103+ if ( response . statusCode != 200 ) {
104+ reject ( ) ;
105+ } else {
106+ //ensure that the user can call `then()` only when the file has
107+ //been downloaded entirely.
108+ response . pipe ( writer ) ;
109+ let error = null ;
110+ writer . on ( 'error' , err => {
111+ error = err ;
112+ writer . close ( ) ;
113+ reject ( err ) ;
114+ } ) ;
115+ writer . on ( 'close' , async ( ) => {
116+ if ( ! error ) {
117+ await unzipFile ( filePath , fileName ) ;
118+ fs . unlinkSync ( tmpFilePath ) ;
119+ resolve ( true ) ;
120+ }
121+ //no need to call the reject here, as it will have been called in the
122+ //'error' stream;
123+ } ) ;
124+ }
125+ } ) ;
130126 } ) ;
131127}
132128
@@ -140,55 +136,50 @@ const unzipFile = async (filePath, fileName) => {
140136}
141137
142138const sendUpdatesToBstack = async ( bsConfig , buildId , args , options , rawArgs ) => {
143- options . url = `${ config . buildUrl } ${ buildId } /build_artifacts/status` ;
139+ options . url = `${ config . buildUrl } ${ buildId } /build_artifacts/status` ;
144140
145- let cypressJSON = utils . getCypressJSON ( bsConfig ) ;
141+ let cypressJSON = utils . getCypressJSON ( bsConfig ) ;
146142
147- let reporter = null ;
148- if ( ! utils . isUndefined ( args . reporter ) ) {
149- reporter = args . reporter ;
150- } else if ( cypressJSON !== undefined ) {
151- reporter = cypressJSON . reporter ;
152- }
143+ let reporter = null ;
144+ if ( ! utils . isUndefined ( args . reporter ) ) {
145+ reporter = args . reporter ;
146+ } else if ( cypressJSON !== undefined ) {
147+ reporter = cypressJSON . reporter ;
148+ }
153149
154- let data = {
155- feature_usage : {
156- downloads : {
157- eligible_download_folders : BUILD_ARTIFACTS_TOTAL_COUNT ,
158- successfully_downloaded_folders : BUILD_ARTIFACTS_TOTAL_COUNT - BUILD_ARTIFACTS_FAIL_COUNT
159- } ,
160- reporter : reporter
161- }
150+ let data = {
151+ feature_usage : {
152+ downloads : {
153+ eligible_download_folders : BUILD_ARTIFACTS_TOTAL_COUNT ,
154+ successfully_downloaded_folders : BUILD_ARTIFACTS_TOTAL_COUNT - BUILD_ARTIFACTS_FAIL_COUNT
155+ } ,
156+ reporter : reporter
162157 }
158+ }
163159
164- options . formData = data . toString ( ) ;
165- let responseData = null ;
166- return new Promise ( async ( resolve , reject ) => {
167- try {
168- request . post ( options , function ( err , resp , data ) {
169- if ( err ) {
170- utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
171- reject ( err ) ;
172- } else {
173- try {
174- responseData = JSON . parse ( data ) ;
175- } catch ( e ) {
176- responseData = { } ;
177- }
178- if ( resp . statusCode != 200 ) {
179- if ( responseData && responseData [ "error" ] ) {
180- utils . sendUsageReport ( bsConfig , args , responseData [ "error" ] , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
181- reject ( responseData [ "error" ] )
182- }
183- }
184- }
185- resolve ( )
186- } ) ;
187- } catch ( err ) {
160+ options . formData = data . toString ( ) ;
161+ let responseData = null ;
162+ return new Promise ( async ( resolve , reject ) => {
163+ request . post ( options , function ( err , resp , data ) {
164+ if ( err ) {
188165 utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
189166 reject ( err ) ;
167+ } else {
168+ try {
169+ responseData = JSON . parse ( data ) ;
170+ } catch ( e ) {
171+ responseData = { } ;
172+ }
173+ if ( resp . statusCode != 200 ) {
174+ if ( responseData && responseData [ "error" ] ) {
175+ utils . sendUsageReport ( bsConfig , args , responseData [ "error" ] , Constants . messageTypes . ERROR , 'api_failed_build_artifacts_status_update' , null , rawArgs ) ;
176+ reject ( responseData [ "error" ] )
177+ }
178+ }
190179 }
180+ resolve ( )
191181 } ) ;
182+ } ) ;
192183}
193184
194185exports . downloadBuildArtifacts = async ( bsConfig , buildId , args , rawArgs ) => {
@@ -210,56 +201,50 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args, rawArgs) => {
210201 let message = null ;
211202 let messageType = null ;
212203 let errorCode = null ;
213-
214- try {
215- let buildDetails = null ;
216- request . get ( options , async function ( err , resp , body ) {
217- if ( err ) {
218- utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
219- process . exitCode = Constants . ERROR_EXIT_CODE ;
220- } else {
221- try {
222- buildDetails = JSON . parse ( body ) ;
223- if ( resp . statusCode != 200 ) {
224- logger . error ( 'Downloading the build artifacts failed.' ) ;
225- logger . error ( `Error: Request failed with status code ${ resp . statusCode } ` )
226- utils . sendUsageReport ( bsConfig , args , buildDetails , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
227- process . exitCode = Constants . ERROR_EXIT_CODE ;
228- } else {
229- await createDirectories ( buildId , buildDetails ) ;
230- await parseAndDownloadArtifacts ( buildId , buildDetails ) ;
231- if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
232- messageType = Constants . messageTypes . ERROR ;
233- message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
234- logger . error ( message ) ;
235- process . exitCode = Constants . ERROR_EXIT_CODE ;
236- } else {
237- messageType = Constants . messageTypes . SUCCESS ;
238- message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_SUCCESS . replace ( '<build-id>' , buildId ) . replace ( '<user-path>' , process . cwd ( ) ) ;
239- logger . info ( message ) ;
240- }
241- await sendUpdatesToBstack ( bsConfig , buildId , args , options , rawArgs )
242- utils . sendUsageReport ( bsConfig , args , message , messageType , null , null , rawArgs ) ;
243- }
244- } catch ( err ) {
245- messageType = Constants . messageTypes . ERROR ;
246- errorCode = 'api_failed_build_artifacts' ;
204+ let buildDetails = null ;
205+ request . get ( options , async function ( err , resp , body ) {
206+ if ( err ) {
207+ utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
208+ process . exitCode = Constants . ERROR_EXIT_CODE ;
209+ } else {
210+ try {
211+ buildDetails = JSON . parse ( body ) ;
212+ if ( resp . statusCode != 200 ) {
213+ logger . error ( 'Downloading the build artifacts failed.' ) ;
214+ logger . error ( `Error: Request failed with status code ${ resp . statusCode } ` )
215+ utils . sendUsageReport ( bsConfig , args , buildDetails , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
216+ process . exitCode = Constants . ERROR_EXIT_CODE ;
217+ } else {
218+ await createDirectories ( buildId , buildDetails ) ;
219+ await parseAndDownloadArtifacts ( buildId , buildDetails ) ;
247220 if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
248221 messageType = Constants . messageTypes . ERROR ;
249222 message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
250223 logger . error ( message ) ;
224+ process . exitCode = Constants . ERROR_EXIT_CODE ;
251225 } else {
252- logger . error ( 'Downloading the build artifacts failed.' ) ;
226+ messageType = Constants . messageTypes . SUCCESS ;
227+ message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_SUCCESS . replace ( '<build-id>' , buildId ) . replace ( '<user-path>' , process . cwd ( ) ) ;
228+ logger . info ( message ) ;
253229 }
254- utils . sendUsageReport ( bsConfig , args , err , messageType , errorCode , null , rawArgs ) ;
255- logger . error ( `Error: Request failed with status code ${ resp . statusCode } ` )
256- process . exitCode = Constants . ERROR_EXIT_CODE ;
230+ await sendUpdatesToBstack ( bsConfig , buildId , args , options , rawArgs )
231+ utils . sendUsageReport ( bsConfig , args , message , messageType , null , null , rawArgs ) ;
232+ }
233+ } catch ( err ) {
234+ messageType = Constants . messageTypes . ERROR ;
235+ errorCode = 'api_failed_build_artifacts' ;
236+ if ( BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
237+ messageType = Constants . messageTypes . ERROR ;
238+ message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , BUILD_ARTIFACTS_FAIL_COUNT ) ;
239+ logger . error ( message ) ;
240+ } else {
241+ logger . error ( 'Downloading the build artifacts failed.' ) ;
257242 }
243+ utils . sendUsageReport ( bsConfig , args , err , messageType , errorCode , null , rawArgs ) ;
244+ logger . error ( `Error: Request failed with status code ${ resp . statusCode } ` )
245+ process . exitCode = Constants . ERROR_EXIT_CODE ;
258246 }
259- } ) ;
260- } catch ( err ) {
261- utils . sendUsageReport ( bsConfig , args , err , Constants . messageTypes . ERROR , 'api_failed_build_artifacts' , null , rawArgs ) ;
262- process . exitCode = Constants . ERROR_EXIT_CODE ;
263- }
247+ }
248+ } ) ;
264249 } ) ;
265250} ;
0 commit comments