@@ -58,34 +58,33 @@ export abstract class Emulator {
58
58
const filepath : string = path . resolve ( dir , this . binaryName ) ;
59
59
const writer = fs . createWriteStream ( filepath ) ;
60
60
console . log ( `Downloading emulator from [${ this . binaryUrl } ] ...` ) ;
61
- try {
62
- fetch ( this . binaryUrl ) . then ( resp => {
63
- const reader = resp . body ?. getReader ( ) ;
64
- reader ?. read ( ) . then ( function readStuff ( this : Emulator , { done, value } ) : any {
65
- if ( done ) {
66
- console . log ( `Saved emulator binary file to [${ filepath } ].` ) ;
67
- // Change emulator binary file permission to 'rwxr-xr-x'.
68
- // The execute permission is required for it to be able to start
69
- // with 'java -jar'.
70
- fs . chmod ( filepath , 0o755 , err => {
71
- if ( err ) reject ( err ) ;
72
- console . log ( `Changed emulator file permissions to 'rwxr-xr-x'.` ) ;
73
- ( this as Emulator ) . setBinaryPath ( filepath ) ; //this.binaryPath = filepath;
74
- if ( this . copyToCache ( ) ) {
75
- console . log ( `Cached emulator at ${ this . cacheBinaryPath } ` ) ;
76
- }
77
- resolve ( ) ;
78
- } ) ;
79
- } else {
80
- writer . write ( value ) ;
81
- return reader . read ( ) . then ( readStuff ) ;
82
- }
61
+ const downloadPromise = new Promise ( ( downloadComplete , downloadFailed ) => {
62
+ try {
63
+ fetch ( this . binaryUrl ) . then ( resp => {
64
+ const reader = resp . body ?. getReader ( ) ;
65
+ reader ?. read ( ) . then ( function readStuff ( { done, value } ) : any {
66
+ if ( done ) {
67
+ downloadComplete ;
68
+ } else {
69
+ writer . write ( value ) ;
70
+ return reader . read ( ) . then ( readStuff ) ;
71
+ }
72
+ } ) ;
83
73
} ) ;
84
- } ) ;
85
- } catch ( e ) {
86
- console . log ( `Download of emulator failed: ${ e } ` ) ;
87
- reject ( ) ;
88
- }
74
+ } catch ( e ) {
75
+ console . log ( `Download of emulator failed: ${ e } ` ) ;
76
+ downloadFailed ( ) ;
77
+ }
78
+ } ) ;
79
+ downloadPromise . then ( ( ) => {
80
+ this . binaryPath = filepath ;
81
+ if ( this . copyToCache ( ) ) {
82
+ console . log ( `Cached emulator at ${ this . cacheBinaryPath } ` ) ;
83
+ }
84
+ resolve ;
85
+ } , ( ) => {
86
+ reject ;
87
+ } ) ;
89
88
} ) ;
90
89
} ) ;
91
90
}
0 commit comments