|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import { cpSync, existsSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import { VerifyFailure, type Platform } from './core/types.ts'; |
| 6 | +import { resolveJavaHome, sdkRoot } from './platform/android/sdk.ts'; |
| 7 | + |
| 8 | +export const APP_ID = 'com.clerk.exponativebuildfixture'; |
| 9 | +export const IOS_PRODUCT = 'ClerkExpoNativeBuildFixture'; |
| 10 | +export const WORKTREE = fileURLToPath(new URL('../../../../', import.meta.url)); |
| 11 | +export const FIXTURE = join(WORKTREE, 'integration', 'templates', 'expo-native'); |
| 12 | + |
| 13 | +const EXPO_PACKAGES = [ |
| 14 | + 'expo-auth-session', |
| 15 | + 'expo-constants', |
| 16 | + 'expo-crypto', |
| 17 | + 'expo-dev-client', |
| 18 | + 'expo-secure-store', |
| 19 | + 'expo-web-browser', |
| 20 | +]; |
| 21 | + |
| 22 | +const SHARED_NATIVE_INPUTS = [ |
| 23 | + 'packages/expo/app.plugin.js', |
| 24 | + 'packages/expo/src/specs', |
| 25 | + 'packages/expo/expo-module.config.json', |
| 26 | + 'packages/expo/react-native.config.js', |
| 27 | + 'packages/expo/package.json', |
| 28 | + 'packages/expo-google-signin/app.plugin.js', |
| 29 | + 'packages/expo-google-signin/expo-module.config.json', |
| 30 | + 'packages/expo-google-signin/package.json', |
| 31 | + 'packages/expo-biometrics/expo-module.config.json', |
| 32 | + 'packages/expo-biometrics/package.json', |
| 33 | + 'integration/templates/expo-native/app.json', |
| 34 | + 'integration/templates/expo-native/app.config.js', |
| 35 | + 'integration/templates/expo-native/package.sdk-57.json', |
| 36 | + 'integration/templates/expo-native/pnpm-workspace.yaml', |
| 37 | + 'integration/templates/expo-native/modules', |
| 38 | +] as const; |
| 39 | + |
| 40 | +const PLATFORM_NATIVE_INPUTS: Readonly<Record<Platform, readonly string[]>> = { |
| 41 | + ios: ['packages/expo/ios', 'packages/expo-google-signin/ios', 'packages/expo-biometrics/ios'], |
| 42 | + android: ['packages/expo/android', 'packages/expo-google-signin/android', 'packages/expo-biometrics/android'], |
| 43 | +}; |
| 44 | + |
| 45 | +export function nativeInputs(platform: Platform): readonly string[] { |
| 46 | + return [...PLATFORM_NATIVE_INPUTS[platform], ...SHARED_NATIVE_INPUTS]; |
| 47 | +} |
| 48 | + |
| 49 | +export function artifact(platform: Platform): string { |
| 50 | + return platform === 'ios' |
| 51 | + ? join(FIXTURE, 'ios', 'build', 'Build', 'Products', 'Debug-iphonesimulator', `${IOS_PRODUCT}.app`) |
| 52 | + : join(FIXTURE, 'android', 'app', 'build', 'outputs', 'apk', 'debug', 'app-debug.apk'); |
| 53 | +} |
| 54 | + |
| 55 | +function step( |
| 56 | + command: string, |
| 57 | + args: readonly string[], |
| 58 | + cwd: string, |
| 59 | + env: Readonly<Record<string, string>> = {}, |
| 60 | +): Promise<{ code: number; tail: string }> { |
| 61 | + return new Promise(resolve => { |
| 62 | + const child = spawn(command, [...args], { |
| 63 | + cwd, |
| 64 | + env: { ...process.env, LANG: 'en_US.UTF-8', CI: '1', ...env }, |
| 65 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 66 | + }); |
| 67 | + const lines: string[] = []; |
| 68 | + const collect = (chunk: Buffer) => { |
| 69 | + for (const line of chunk.toString().split('\n')) { |
| 70 | + if (line.trim().length === 0) continue; |
| 71 | + lines.push(line); |
| 72 | + if (lines.length > 40) lines.shift(); |
| 73 | + } |
| 74 | + }; |
| 75 | + child.stdout.on('data', collect); |
| 76 | + child.stderr.on('data', collect); |
| 77 | + child.on('error', () => resolve({ code: 127, tail: `${command} could not start` })); |
| 78 | + child.on('close', code => resolve({ code: code ?? 1, tail: lines.slice(-15).join('\n') })); |
| 79 | + }); |
| 80 | +} |
| 81 | + |
| 82 | +export async function mustStep( |
| 83 | + what: string, |
| 84 | + command: string, |
| 85 | + args: readonly string[], |
| 86 | + cwd: string, |
| 87 | + env?: Readonly<Record<string, string>>, |
| 88 | +): Promise<void> { |
| 89 | + const result = await step(command, args, cwd, env); |
| 90 | + if (result.code !== 0) |
| 91 | + throw new VerifyFailure( |
| 92 | + 'BUILD_FAILED', |
| 93 | + `${what} exited ${result.code}:\n${result.tail}`, |
| 94 | + 'fix the error above, then rerun {cli} up', |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export interface FixtureBuild { |
| 99 | + readonly platform: Platform; |
| 100 | + readonly buildPackages: boolean; |
| 101 | + readonly progress: (line: string) => void; |
| 102 | +} |
| 103 | + |
| 104 | +async function generateNativeProject(build: FixtureBuild): Promise<void> { |
| 105 | + const { platform, progress } = build; |
| 106 | + cpSync(join(FIXTURE, 'package.sdk-57.json'), join(FIXTURE, 'package.json')); |
| 107 | + await mustStep( |
| 108 | + 'pnpm add the workspace packages', |
| 109 | + 'pnpm', |
| 110 | + [ |
| 111 | + 'add', |
| 112 | + 'link:../../../packages/expo', |
| 113 | + 'link:../../../packages/expo-google-signin', |
| 114 | + 'link:../../../packages/expo-biometrics', |
| 115 | + ], |
| 116 | + FIXTURE, |
| 117 | + ); |
| 118 | + await mustStep('expo install', 'pnpm', ['expo', 'install', ...EXPO_PACKAGES], FIXTURE); |
| 119 | + progress(`build expo prebuild --clean --platform ${platform}`); |
| 120 | + await mustStep('expo prebuild', 'pnpm', ['expo', 'prebuild', '--clean', '--platform', platform], FIXTURE); |
| 121 | +} |
| 122 | + |
| 123 | +export async function buildFixture(build: FixtureBuild): Promise<string> { |
| 124 | + const { platform, progress } = build; |
| 125 | + if (!existsSync(join(WORKTREE, 'node_modules'))) { |
| 126 | + throw new VerifyFailure('NOT_READY', 'the monorepo has no node_modules', `cd ${WORKTREE} && pnpm install`); |
| 127 | + } |
| 128 | + if (build.buildPackages) { |
| 129 | + progress('build turbo build @clerk/expo, @clerk/expo-biometrics, @clerk/expo-google-signin'); |
| 130 | + await mustStep( |
| 131 | + 'turbo build', |
| 132 | + 'pnpm', |
| 133 | + [ |
| 134 | + 'turbo', |
| 135 | + 'build', |
| 136 | + '--filter=@clerk/expo...', |
| 137 | + '--filter=@clerk/expo-biometrics...', |
| 138 | + '--filter=@clerk/expo-google-signin...', |
| 139 | + ], |
| 140 | + WORKTREE, |
| 141 | + ); |
| 142 | + } |
| 143 | + await generateNativeProject(build); |
| 144 | + if (platform === 'ios') { |
| 145 | + progress('build xcodebuild Debug (dev client)'); |
| 146 | + await mustStep( |
| 147 | + 'xcodebuild', |
| 148 | + 'xcodebuild', |
| 149 | + [ |
| 150 | + 'build', |
| 151 | + '-quiet', |
| 152 | + '-workspace', |
| 153 | + `ios/${IOS_PRODUCT}.xcworkspace`, |
| 154 | + '-scheme', |
| 155 | + IOS_PRODUCT, |
| 156 | + '-configuration', |
| 157 | + 'Debug', |
| 158 | + '-sdk', |
| 159 | + 'iphonesimulator', |
| 160 | + '-derivedDataPath', |
| 161 | + join(FIXTURE, 'ios', 'build'), |
| 162 | + 'CODE_SIGN_IDENTITY=-', |
| 163 | + ], |
| 164 | + FIXTURE, |
| 165 | + ); |
| 166 | + } else { |
| 167 | + const java = resolveJavaHome(); |
| 168 | + if (!java.ok) throw new VerifyFailure('NOT_READY', java.detail, java.fix); |
| 169 | + progress('build gradlew assembleDebug (dev client)'); |
| 170 | + await mustStep('gradlew assembleDebug', './gradlew', ['assembleDebug', '-q'], join(FIXTURE, 'android'), { |
| 171 | + JAVA_HOME: java.home, |
| 172 | + ANDROID_HOME: sdkRoot(), |
| 173 | + }); |
| 174 | + } |
| 175 | + return artifact(platform); |
| 176 | +} |
0 commit comments