import * as fs from 'fs' ; import * as request from 'request' ; import { LoadingBar } from './ProgressBar' ; export function downloadSDK ( env , url , to , callback ) { let fileStream = fs . createWriteStream ( to ) ; let loading = new LoadingBar ( ) ; let receivedBytes = 0 ; request . get ( { url : url , headers : { 'User-Agent' : 'request' } } ) . on ( 'response' , response => { if ( response . statusCode === 200 ) { loading . start ( ) ; } else { console . log ( "error" ) ; } } ) . on ( 'data' , ( chunk ) => { receivedBytes += chunk . length ; } ) . on ( 'end' , ( code ) => { loading . stop ( ) ; } ) . on ( 'error' , ( err ) => { fs . unlink ( local_file ) ; loading . stop ( ) ; } ) . pipe ( fileStream ) ; }