Skip to content

Commit 45723bb

Browse files
committed
Draft emulator impl not quite working.
1 parent 4bdab6f commit 45723bb

File tree

1 file changed

+33
-26
lines changed

1 file changed

+33
-26
lines changed

scripts/emulator-testing/emulators/emulator.ts

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import * as os from 'os';
2323
import * as path from 'path';
2424
// @ts-ignore
2525
import * as tmp from 'tmp';
26+
import { Readable } from "stream";
2627

2728
export abstract class Emulator {
2829
binaryPath: string | null = null;
@@ -40,6 +41,9 @@ export abstract class Emulator {
4041
this.cacheBinaryPath = path.join(this.cacheDirectory, binaryName);
4142
}
4243

44+
setBinaryPath(path: string) {
45+
this.binaryPath = path;
46+
}
4347
download(): Promise<void> {
4448
if (fs.existsSync(this.cacheBinaryPath)) {
4549
console.log(`Emulator found in cache: ${this.cacheBinaryPath}`);
@@ -50,35 +54,38 @@ export abstract class Emulator {
5054
return new Promise<void>((resolve, reject) => {
5155
tmp.dir((err: Error | null, dir: string) => {
5256
if (err) reject(err);
53-
5457
console.log(`Created temporary directory at [${dir}].`);
5558
const filepath: string = path.resolve(dir, this.binaryName);
56-
const writeStream: fs.WriteStream = fs.createWriteStream(filepath);
57-
59+
const writer = fs.createWriteStream(filepath);
5860
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+
}
8289
});
8390
});
8491
}

0 commit comments

Comments
 (0)