Skip to content

Commit

Permalink
Refactor ensureExpoClientInstalledAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldonadel committed Jan 2, 2024
1 parent 2233b0f commit 0267046
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
3 changes: 2 additions & 1 deletion apps/cli/src/commands/LaunchSnack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export async function getSDKVersionForSnack(snackURL: string): Promise<string |
'Snack-Api-Version': '3.0.0',
},
});
const { sdkVersion } = await response.json();
const { sdkVersion }: { sdkVersion: string } = await response.json();

return sdkVersion;
} catch (err) {
console.error(`Failed fetch snack with identifier: ${snackId}`, err);
throw err;
}
}
65 changes: 28 additions & 37 deletions packages/eas-shared/src/run/android/emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,16 @@ export async function startAppAsync(
Log.succeed('Successfully started your app!');
}

// Expo installed
async function isExpoClientInstalledOnEmulatorAsync(pid: string): Promise<boolean> {
const packages = await adbAsync(
'-s',
pid,
'shell',
'pm',
'list',
'packages',
EXPO_GO_BUNDLE_IDENTIFIER
);
export async function isAppInstalledOnEmulatorAsync(
pid: string,
bundleId: string
): Promise<boolean> {
const packages = await adbAsync('-s', pid, 'shell', 'pm', 'list', 'packages', bundleId);

const lines = packages.stdout.split(/\r?\n/);
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (line === `package:${EXPO_GO_BUNDLE_IDENTIFIER}`) {
if (line === `package:${bundleId}`) {
return true;
}
}
Expand Down Expand Up @@ -325,7 +319,7 @@ async function checkExpoClientCompatibilityAsync(

const installedVersion = await expoVersionOnEmulatorAsync(pid);
if (!installedVersion) {
return { compatible: true };
return { compatible: false };
}

const isCompatible = semver.satisfies(
Expand Down Expand Up @@ -377,32 +371,29 @@ export async function installExpoOnEmulatorAsync({
}

export async function ensureExpoClientInstalledAsync(pid: string, sdkVersion?: string) {
let isCompatible = true;
let requiresDowngrade = false;

const isInstalled = await isExpoClientInstalledOnEmulatorAsync(pid);
if (isInstalled) {
const compatibility = await checkExpoClientCompatibilityAsync(pid, sdkVersion);
isCompatible = compatibility.compatible;
requiresDowngrade = compatibility.requiresDowngrade ?? false;
}
const { compatible, requiresDowngrade } = await checkExpoClientCompatibilityAsync(
pid,
sdkVersion
);

if (!isInstalled || !isCompatible) {
if (requiresDowngrade) {
await uninstallAppAsync({ pid } as AndroidEmulator, EXPO_GO_BUNDLE_IDENTIFIER);
}
if (compatible) {
return;
}

const androidClient = await getClientForSDK(sdkVersion);
const versions = await Versions.versionsAsync();
const url = androidClient?.url ?? versions.androidUrl;
if (!url) {
throw new Error();
}
if (requiresDowngrade) {
await uninstallAppAsync({ pid } as AndroidEmulator, EXPO_GO_BUNDLE_IDENTIFIER);
}

await installExpoOnEmulatorAsync({
pid,
url,
version: androidClient?.version,
});
const androidClient = await getClientForSDK(sdkVersion);
const versions = await Versions.versionsAsync();
const url = androidClient?.url ?? versions.androidUrl;
if (!url) {
throw new Error('Unable to determine Expo Go download URL');
}

await installExpoOnEmulatorAsync({
pid,
url,
version: androidClient?.version,
});
}

0 comments on commit 0267046

Please sign in to comment.