Skip to content

Commit 5ee8eb7

Browse files
committed
Wrap the emulator download in a promise
1 parent 45723bb commit 5ee8eb7

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

scripts/emulator-testing/emulators/emulator.ts

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,33 @@ export abstract class Emulator {
5858
const filepath: string = path.resolve(dir, this.binaryName);
5959
const writer = fs.createWriteStream(filepath);
6060
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+
});
8373
});
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+
});
8988
});
9089
});
9190
}

0 commit comments

Comments
 (0)