@@ -23,6 +23,7 @@ import * as os from 'os';
23
23
import * as path from 'path' ;
24
24
// @ts -ignore
25
25
import * as tmp from 'tmp' ;
26
+ import { Readable } from "stream" ;
26
27
27
28
export abstract class Emulator {
28
29
binaryPath : string | null = null ;
@@ -40,6 +41,9 @@ export abstract class Emulator {
40
41
this . cacheBinaryPath = path . join ( this . cacheDirectory , binaryName ) ;
41
42
}
42
43
44
+ setBinaryPath ( path : string ) {
45
+ this . binaryPath = path ;
46
+ }
43
47
download ( ) : Promise < void > {
44
48
if ( fs . existsSync ( this . cacheBinaryPath ) ) {
45
49
console . log ( `Emulator found in cache: ${ this . cacheBinaryPath } ` ) ;
@@ -50,35 +54,38 @@ export abstract class Emulator {
50
54
return new Promise < void > ( ( resolve , reject ) => {
51
55
tmp . dir ( ( err : Error | null , dir : string ) => {
52
56
if ( err ) reject ( err ) ;
53
-
54
57
console . log ( `Created temporary directory at [${ dir } ].` ) ;
55
58
const filepath : string = path . resolve ( dir , this . binaryName ) ;
56
- const writeStream : fs . WriteStream = fs . createWriteStream ( filepath ) ;
57
-
59
+ const writer = fs . createWriteStream ( filepath ) ;
58
60
console . log ( `Downloading emulator from [${ this . binaryUrl } ] ...` ) ;
59
- fetch ( this . binaryUrl ) . then ( resp => {
60
- resp . body
61
- . pipe ( writeStream )
62
- . on ( 'finish' , ( ) => {
63
- console . log ( `Saved emulator binary file to [${ filepath } ].` ) ;
64
- // Change emulator binary file permission to 'rwxr-xr-x'.
65
- // The execute permission is required for it to be able to start
66
- // with 'java -jar'.
67
- fs . chmod ( filepath , 0o755 , err => {
68
- if ( err ) reject ( err ) ;
69
- console . log (
70
- `Changed emulator file permissions to 'rwxr-xr-x'.`
71
- ) ;
72
- this . binaryPath = filepath ;
73
-
74
- if ( this . copyToCache ( ) ) {
75
- console . log ( `Cached emulator at ${ this . cacheBinaryPath } ` ) ;
76
- }
77
- resolve ( ) ;
78
- } ) ;
79
- } )
80
- . on ( 'error' , reject ) ;
81
- } ) ;
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
+ }
83
+ } ) ;
84
+ } ) ;
85
+ } catch ( e ) {
86
+ console . log ( `Download of emulator failed: ${ e } ` ) ;
87
+ reject ( ) ;
88
+ }
82
89
} ) ;
83
90
} ) ;
84
91
}
0 commit comments