Skip to content

Commit

Permalink
[eas-cli] Use expo-updates runtime version CLI to generate runtime ve…
Browse files Browse the repository at this point in the history
…rsions
  • Loading branch information
wschurman committed Feb 24, 2024
1 parent 00e175b commit bdb7cb0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 32 deletions.
6 changes: 2 additions & 4 deletions packages/eas-cli/src/build/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Updates } from '@expo/config-plugins';
import { Metadata, Platform, sanitizeMetadata } from '@expo/eas-build-job';
import { IosEnterpriseProvisioning } from '@expo/eas-json';
import fs from 'fs-extra';
Expand All @@ -15,6 +14,7 @@ import {
isClassicUpdatesSupportedAsync,
isExpoUpdatesInstalled,
} from '../project/projectUtils';
import { resolveRuntimeVersionAsync } from '../project/resolveRuntimeVersionAsync';
import {
readChannelSafelyAsync as readAndroidChannelSafelyAsync,
readReleaseChannelSafelyAsync as readAndroidReleaseChannelSafelyAsync,
Expand All @@ -37,9 +37,7 @@ export async function collectMetadataAsync<T extends Platform>(
workflow: ctx.workflow,
credentialsSource: ctx.buildProfile.credentialsSource,
sdkVersion: ctx.exp.sdkVersion,
runtimeVersion:
(await Updates.getRuntimeVersionNullableAsync(ctx.projectDir, ctx.exp, ctx.platform)) ??
undefined,
runtimeVersion: (await resolveRuntimeVersionAsync(ctx)) ?? undefined,
reactNativeVersion: await getReactNativeVersionAsync(ctx.projectDir),
...channelOrReleaseChannel,
distribution,
Expand Down
32 changes: 9 additions & 23 deletions packages/eas-cli/src/project/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,31 +717,17 @@ async function getRuntimeVersionForPlatformAsync({
return 'UNVERSIONED';
}

try {
const runtimeString = await expoUpdatesCommandAsync(projectDir, [
'runtimeversion:resolve',
'--platform',
platform,
]);
return nullthrows(runtimeString);
} catch {}

const runtimeVersion = exp[platform]?.runtimeVersion ?? exp.runtimeVersion;
if (typeof runtimeVersion === 'object') {
const policy = runtimeVersion.policy;

if (policy === 'fingerprintExperimental') {
// log to inform the user that the fingerprint has been calculated
Log.warn(
`Calculating native fingerprint for platform ${platform} using current state of the "${platform}" directory. ` +
`If the fingerprint differs from the build's fingerint, ensure the state of your project is consistent ` +
`(repository is clean, ios and android native directories are in the same state as the build if applicable).`
);

const fingerprintRawString = await expoUpdatesCommandAsync(projectDir, [
'fingerprint:generate',
'--platform',
platform,
]);
const fingerprintObject = JSON.parse(fingerprintRawString);
const hash = nullthrows(
fingerprintObject.hash,
'invalid response from expo-update CLI for fingerprint generation'
);
return hash;
}

const workflow = await resolveWorkflowAsync(
projectDir,
platform as EASBuildJobPlatform,
Expand Down
33 changes: 33 additions & 0 deletions packages/eas-cli/src/project/resolveRuntimeVersionAsync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ExpoConfig } from '@expo/config';
import { Updates } from '@expo/config-plugins';

import { ModuleNotFoundError, expoUpdatesCommandAsync } from '../utils/expoUpdatesCli';

export async function resolveRuntimeVersionAsync({
exp,
platform,
projectDir,
}: {
exp: ExpoConfig;
platform: 'ios' | 'android';
projectDir: string;
}): Promise<string | null> {
try {
const runtimeJSONString = await expoUpdatesCommandAsync(projectDir, [
'runtimeversion:resolve',
'--platform',
platform,
]);
const runtimeVersion = JSON.parse(runtimeJSONString).runtimeVersion;
return runtimeVersion ?? null;
} catch (e: any) {
// if expo-updates is not installed, there's no need for a runtime version in the build
if (e instanceof ModuleNotFoundError) {
return null;
}

// fall back to the previous behavior (using the @expo/config-plugins eas-cli dependency rather
// than the versioned @expo/config-plugins dependency in the project)
return await Updates.getRuntimeVersionNullableAsync(projectDir, exp, platform);
}
}
4 changes: 2 additions & 2 deletions packages/eas-cli/src/rollout/actions/CreateRollout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Updates } from '@expo/config-plugins';
import assert from 'assert';

import { SelectRuntime } from './SelectRuntime';
Expand All @@ -24,6 +23,7 @@ import {
} from '../../graphql/queries/ChannelQuery';
import { UpdateQuery } from '../../graphql/queries/UpdateQuery';
import Log from '../../log';
import { resolveRuntimeVersionAsync } from '../../project/resolveRuntimeVersionAsync';
import { confirmAsync, promptAsync } from '../../prompts';
import { truthy } from '../../utils/expodash/filter';
import {
Expand Down Expand Up @@ -275,7 +275,7 @@ export class CreateRollout implements EASUpdateAction<UpdateChannelBasicInfoFrag
const runtimes = (
await Promise.all(
platforms.map(platform =>
Updates.getRuntimeVersionAsync(ctx.app.projectDir, ctx.app.exp, platform)
resolveRuntimeVersionAsync({ projectDir: ctx.app.projectDir, exp: ctx.app.exp, platform })
)
)
).filter(truthy);
Expand Down
9 changes: 6 additions & 3 deletions packages/eas-cli/src/utils/expoUpdatesCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import resolveFrom, { silent as silentResolveFrom } from 'resolve-from';

import Log, { link } from '../log';

export class ModuleNotFoundError extends Error {}

export async function expoUpdatesCommandAsync(projectDir: string, args: string[]): Promise<string> {
let expoUpdatesCli;
try {
Expand All @@ -12,7 +14,7 @@ export async function expoUpdatesCommandAsync(projectDir: string, args: string[]
resolveFrom(projectDir, 'expo-updates/bin/cli.js');
} catch (e: any) {
if (e.code === 'MODULE_NOT_FOUND') {
throw new Error(
throw new ModuleNotFoundError(
`The \`expo-updates\` package was not found. Follow the installation directions at ${link(
'https://docs.expo.dev/bare/installing-expo-modules/'
)}`
Expand All @@ -22,17 +24,18 @@ export async function expoUpdatesCommandAsync(projectDir: string, args: string[]
}

const spawnPromise = spawnAsync(expoUpdatesCli, args, {
stdio: ['inherit', 'pipe', 'pipe'], // inherit stdin so user can install a missing expo-cli from inside this command
stdio: ['inherit', 'pipe', 'pipe'],
});
const {
child: { stderr },
} = spawnPromise;
if (!stderr) {
throw new Error('Failed to spawn expo-updates cli');
}

stderr.on('data', data => {
for (const line of data.toString().trim().split('\n')) {
Log.warn(`${chalk.gray('[expo-cli]')} ${line}`);
Log.debug(`${chalk.gray('[expo-updates-cli]')} ${line}`);
}
});
const result = await spawnPromise;
Expand Down

0 comments on commit bdb7cb0

Please sign in to comment.