|
| 1 | +import { Android, BuildJob, Ios, Platform } from '@expo/eas-build-job'; |
| 2 | +import { PipeMode } from '@expo/logger'; |
| 3 | +import fs from 'fs-extra'; |
| 4 | +import os from 'os'; |
| 5 | +import path from 'path'; |
| 6 | +import StreamZip from 'node-stream-zip'; |
| 7 | + |
| 8 | +import { findArtifacts } from './artifacts'; |
| 9 | +import { runEasCliCommand } from './easCli'; |
| 10 | +import { resolveArtifactPath } from '../ios/resolve'; |
| 11 | +import { BuildContext } from '../context'; |
| 12 | + |
| 13 | +export async function uploadEmbeddedBundleAsync(ctx: BuildContext<BuildJob>): Promise<void> { |
| 14 | + const { platform } = ctx.job; |
| 15 | + const channel = ctx.job.updates?.channel; |
| 16 | + const projectDir = ctx.getReactNativeProjectDirectory(); |
| 17 | + |
| 18 | + const archivePattern = |
| 19 | + platform === Platform.IOS |
| 20 | + ? resolveArtifactPath(ctx as BuildContext<Ios.Job>) |
| 21 | + : ((ctx as BuildContext<Android.Job>).job.applicationArchivePath ?? |
| 22 | + 'android/app/build/outputs/**/*.{apk,aab}'); |
| 23 | + |
| 24 | + const [archivePath] = await findArtifacts({ |
| 25 | + rootDir: projectDir, |
| 26 | + patternOrPath: archivePattern, |
| 27 | + logger: null, |
| 28 | + }).catch(() => [] as string[]); |
| 29 | + |
| 30 | + if (!channel || !archivePath) { |
| 31 | + ctx.logger.warn( |
| 32 | + `Skipping embedded bundle upload: ${!channel ? 'no channel configured for this build profile' : 'build archive not found'}.` |
| 33 | + ); |
| 34 | + ctx.markBuildPhaseHasWarnings(); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'eas-embedded-bundle-')); |
| 39 | + const zip = new StreamZip.async({ file: archivePath }); |
| 40 | + try { |
| 41 | + const entries = Object.values(await zip.entries()); |
| 42 | + const bundleEntry = entries.find(e => |
| 43 | + platform === Platform.IOS |
| 44 | + ? e.name.endsWith('/main.jsbundle') |
| 45 | + : e.name === 'assets/index.android.bundle' |
| 46 | + ); |
| 47 | + const manifestEntry = entries.find(e => |
| 48 | + platform === Platform.IOS |
| 49 | + ? e.name.includes('EXUpdates.bundle/app.manifest') |
| 50 | + : e.name === 'assets/app.manifest' |
| 51 | + ); |
| 52 | + |
| 53 | + if (!bundleEntry || !manifestEntry) { |
| 54 | + ctx.logger.warn('Skipping embedded bundle upload: bundle or manifest not found in archive.'); |
| 55 | + ctx.markBuildPhaseHasWarnings(); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const bundleName = platform === Platform.IOS ? 'main.jsbundle' : 'index.android.bundle'; |
| 60 | + const bundlePath = path.join(tmpDir, bundleName); |
| 61 | + const manifestPath = path.join(tmpDir, 'app.manifest'); |
| 62 | + await zip.extract(bundleEntry.name, bundlePath); |
| 63 | + await zip.extract(manifestEntry.name, manifestPath); |
| 64 | + |
| 65 | + const args = [ |
| 66 | + 'update:embedded:upload', |
| 67 | + '--platform', |
| 68 | + platform, |
| 69 | + '--bundle', |
| 70 | + bundlePath, |
| 71 | + '--manifest', |
| 72 | + manifestPath, |
| 73 | + '--channel', |
| 74 | + channel, |
| 75 | + '--non-interactive', |
| 76 | + ]; |
| 77 | + if (ctx.env.EAS_BUILD_ID) { |
| 78 | + args.push('--build-id', ctx.env.EAS_BUILD_ID); |
| 79 | + } |
| 80 | + await runEasCliCommand({ |
| 81 | + args, |
| 82 | + options: { |
| 83 | + cwd: projectDir, |
| 84 | + env: ctx.env, |
| 85 | + logger: ctx.logger, |
| 86 | + mode: PipeMode.STDERR_ONLY_AS_STDOUT, |
| 87 | + }, |
| 88 | + }); |
| 89 | + } catch (err: any) { |
| 90 | + ctx.logger.warn({ err }, 'Failed to upload embedded bundle.'); |
| 91 | + ctx.markBuildPhaseHasWarnings(); |
| 92 | + } finally { |
| 93 | + await zip.close(); |
| 94 | + } |
| 95 | +} |
0 commit comments