1
1
'use strict' ;
2
2
3
3
const fs = require ( 'fs' ) ,
4
- path = require ( 'path' ) ,
5
- https = require ( 'https' ) ;
4
+ path = require ( 'path' ) ;
6
5
7
6
const axios = require ( 'axios' ) ,
8
7
unzipper = require ( 'unzipper' ) ;
@@ -86,31 +85,48 @@ const createDirectories = async (buildId, data) => {
86
85
}
87
86
88
87
const downloadAndUnzip = async ( filePath , fileName , url ) => {
89
- return new Promise ( async ( resolve , reject ) => {
90
- let tmpFilePath = path . join ( filePath , fileName ) ;
91
- https . get ( url , function ( response ) {
92
- response . on ( 'data' , function ( data ) {
93
- fs . appendFileSync ( tmpFilePath , data ) ;
88
+ let tmpFilePath = path . join ( filePath , fileName ) ;
89
+ const writer = fs . createWriteStream ( tmpFilePath ) ;
90
+
91
+ return axios ( {
92
+ method : 'get' ,
93
+ url : url ,
94
+ responseType : 'stream' ,
95
+ } ) . then ( response => {
96
+
97
+ //ensure that the user can call `then()` only when the file has
98
+ //been downloaded entirely.
99
+
100
+ return new Promise ( async ( resolve , reject ) => {
101
+ response . data . pipe ( writer ) ;
102
+ let error = null ;
103
+ writer . on ( 'error' , err => {
104
+ error = err ;
105
+ writer . close ( ) ;
106
+ reject ( err ) ;
94
107
} ) ;
95
- response . on ( 'end' , function ( ) {
96
- fs . createReadStream ( tmpFilePath ) . pipe ( unzipper . Extract ( { path : filePath } )
97
- . on ( 'close' , function ( ) {
98
- fs . unlinkSync ( tmpFilePath ) ;
99
- resolve ( ) ;
100
- } )
101
- . on ( 'error' , function ( err ) {
102
- process . env . BUILD_ARTIFACTS_FAIL_COUNT = Number ( process . env . BUILD_ARTIFACTS_FAIL_COUNT ) + 1 ;
103
- reject ( err ) ;
104
- } )
105
- ) ;
108
+ writer . on ( 'close' , async ( ) => {
109
+ if ( ! error ) {
110
+ await unzipFile ( filePath , fileName ) ;
111
+ fs . unlinkSync ( tmpFilePath ) ;
112
+ resolve ( true ) ;
113
+ }
114
+ //no need to call the reject here, as it will have been called in the
115
+ //'error' stream;
106
116
} ) ;
107
- response . on ( 'error' , function ( ) {
108
- reject ( ) ;
109
- } )
110
117
} ) ;
111
118
} ) ;
112
119
}
113
120
121
+ const unzipFile = async ( filePath , fileName ) => {
122
+ return new Promise ( async ( resolve , reject ) => {
123
+ await unzipper . Open . file ( path . join ( filePath , fileName ) )
124
+ . then ( d => d . extract ( { path : filePath , concurrency : 5 } ) )
125
+ . catch ( ( err ) => reject ( err ) ) ;
126
+ resolve ( ) ;
127
+ } ) ;
128
+ }
129
+
114
130
const sendUpdatesToBstack = async ( bsConfig , buildId , args , options ) => {
115
131
let url = `${ config . buildUrl } ${ buildId } /build_artifacts/status` ;
116
132
@@ -182,8 +198,15 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args) => {
182
198
messageType = Constants . messageTypes . ERROR ;
183
199
errorCode = 'api_failed_build_artifacts' ;
184
200
185
- logger . error ( 'Downloading the build artifacts failed.' ) ;
186
- logger . error ( err ) ;
201
+ if ( process . env . BUILD_ARTIFACTS_FAIL_COUNT > 0 ) {
202
+ messageType = Constants . messageTypes . ERROR ;
203
+ message = Constants . userMessages . DOWNLOAD_BUILD_ARTIFACTS_FAILED . replace ( '<build-id>' , buildId ) . replace ( '<machine-count>' , process . env . BUILD_ARTIFACTS_FAIL_COUNT ) ;
204
+ logger . error ( message ) ;
205
+ } else {
206
+ logger . error ( 'Downloading the build artifacts failed.' ) ;
207
+ logger . error ( err ) ;
208
+ }
209
+
187
210
utils . sendUsageReport ( bsConfig , args , err , messageType , errorCode ) ;
188
211
}
189
212
} ;
0 commit comments