Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"ini": "^4.1.1",
"plist": "^3.1.0",
"split2": "^4.2.0",
"through2": "^4.0.2",
"tslib": "^2.6.2",
"yauzl": "^2.10.0"
},
Expand All @@ -56,7 +55,6 @@
"@types/node": "^16.0.0",
"@types/plist": "^3.0.4",
"@types/split2": "^4.2.2",
"@types/through2": "^2.0.40",
"@types/yauzl": "^2.10.2",
"eslint": "^8.57.0",
"jest": "^26.4.2",
Expand Down
46 changes: 24 additions & 22 deletions src/android/utils/adb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Debug from 'debug';
import * as os from 'os';
import * as path from 'path';
import * as split2 from 'split2';
import * as through2 from 'through2';
import { Transform } from 'stream';

import {
ADBException,
Expand Down Expand Up @@ -188,27 +188,29 @@ export async function installApk(sdk: SDK, device: Device, apk: string): Promise
});

p.stderr.pipe(split2()).pipe(
through2((chunk, enc, cb) => {
const line = chunk.toString();

debug('adb install: %O', line);
const event = parseAdbInstallOutput(line);

if (event === ADBEvent.IncompatibleUpdateFailure) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_INCOMPATIBLE_UPDATE));
} else if (event === ADBEvent.NewerVersionOnDeviceFailure) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_VERSION_DOWNGRADE));
} else if (event === ADBEvent.NewerSdkRequiredOnDeviceFailure) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_MIN_SDK_VERSION));
} else if (event === ADBEvent.NoCertificates) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NO_CERTIFICATES));
} else if (event === ADBEvent.NotEnoughSpace) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NOT_ENOUGH_SPACE));
} else if (event === ADBEvent.DeviceOffline) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_DEVICE_OFFLINE));
}

cb();
new Transform({
transform: (chunk, enc, cb) => {
const line = chunk.toString();

debug('adb install: %O', line);
const event = parseAdbInstallOutput(line);

if (event === ADBEvent.IncompatibleUpdateFailure) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_INCOMPATIBLE_UPDATE));
} else if (event === ADBEvent.NewerVersionOnDeviceFailure) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_VERSION_DOWNGRADE));
} else if (event === ADBEvent.NewerSdkRequiredOnDeviceFailure) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_MIN_SDK_VERSION));
} else if (event === ADBEvent.NoCertificates) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NO_CERTIFICATES));
} else if (event === ADBEvent.NotEnoughSpace) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_NOT_ENOUGH_SPACE));
} else if (event === ADBEvent.DeviceOffline) {
reject(new ADBException(`Encountered adb error: ${ADBEvent[event]}.`, ERR_DEVICE_OFFLINE));
}

cb();
},
}),
);
});
Expand Down
90 changes: 47 additions & 43 deletions src/android/utils/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as net from 'net';
import * as os from 'os';
import * as path from 'path';
import * as split2 from 'split2';
import * as through2 from 'through2';
import { Transform } from 'stream';

import {
ERR_ALREADY_RUNNING,
Expand Down Expand Up @@ -74,20 +74,22 @@ export async function spawnEmulator(sdk: SDK, avd: AVD, port: number): Promise<v
(err) => reject(err),
);

const eventParser = through2((chunk: string, enc, cb) => {
const line = chunk.toString();

debug('Android Emulator: %O', line);
const event = parseEmulatorOutput(line);
if (event === EmulatorEvent.AlreadyRunning) {
reject(new EmulatorException(`Emulator already running with AVD [${avd.id}]`, ERR_ALREADY_RUNNING));
} else if (event === EmulatorEvent.UnknownAVD) {
reject(new EmulatorException(`Unknown AVD name [${avd.id}]`, ERR_UNKNOWN_AVD));
} else if (event === EmulatorEvent.AVDHomeNotFound) {
reject(new EmulatorException(`Emulator cannot find AVD home`, ERR_AVD_HOME_NOT_FOUND));
}
const eventParser = new Transform({
transform: (chunk: string, enc, cb) => {
const line = chunk.toString();

cb();
debug('Android Emulator: %O', line);
const event = parseEmulatorOutput(line);
if (event === EmulatorEvent.AlreadyRunning) {
reject(new EmulatorException(`Emulator already running with AVD [${avd.id}]`, ERR_ALREADY_RUNNING));
} else if (event === EmulatorEvent.UnknownAVD) {
reject(new EmulatorException(`Unknown AVD name [${avd.id}]`, ERR_UNKNOWN_AVD));
} else if (event === EmulatorEvent.AVDHomeNotFound) {
reject(new EmulatorException(`Emulator cannot find AVD home`, ERR_AVD_HOME_NOT_FOUND));
}

cb();
},
});

const stdoutStream = p.stdout.pipe(split2());
Expand Down Expand Up @@ -199,37 +201,39 @@ export async function getAVDFromEmulator(emulator: Device, avds: readonly AVD[])
});

sock.pipe(split2()).pipe(
through2((chunk: string, enc, cb) => {
const line = chunk.toString();

debug('Android Console: %O', line);

if (stage === Stage.Initial && line.includes('Authentication required')) {
stage = Stage.Auth;
} else if (stage === Stage.Auth && line.trim() === 'OK') {
readAuthFile.then(
(token) => sock.write(`auth ${token}\n`, 'utf8'),
(err) => reject(err),
);
stage = Stage.AuthSuccess;
} else if (stage === Stage.AuthSuccess && line.trim() === 'OK') {
sock.write('avd name\n', 'utf8');
stage = Stage.Response;
} else if (stage === Stage.Response) {
const avdId = line.trim();
const avd = avds.find((avd) => avd.id === avdId);

if (avd) {
resolve(avd);
} else {
reject(new EmulatorException(`Unknown AVD name [${avdId}]`, ERR_UNKNOWN_AVD));
new Transform({
transform: (chunk: string, enc, cb) => {
const line = chunk.toString();

debug('Android Console: %O', line);

if (stage === Stage.Initial && line.includes('Authentication required')) {
stage = Stage.Auth;
} else if (stage === Stage.Auth && line.trim() === 'OK') {
readAuthFile.then(
(token) => sock.write(`auth ${token}\n`, 'utf8'),
(err) => reject(err),
);
stage = Stage.AuthSuccess;
} else if (stage === Stage.AuthSuccess && line.trim() === 'OK') {
sock.write('avd name\n', 'utf8');
stage = Stage.Response;
} else if (stage === Stage.Response) {
const avdId = line.trim();
const avd = avds.find((avd) => avd.id === avdId);

if (avd) {
resolve(avd);
} else {
reject(new EmulatorException(`Unknown AVD name [${avdId}]`, ERR_UNKNOWN_AVD));
}

stage = Stage.Complete;
cleanup();
}

stage = Stage.Complete;
cleanup();
}

cb();
cb();
},
}),
);
});
Expand Down