11import { spawn } from 'node:child_process' ;
2- import { cpSync , existsSync } from 'node:fs' ;
2+ import { createHash } from 'node:crypto' ;
3+ import { cpSync , existsSync , readFileSync , rmSync , writeFileSync } from 'node:fs' ;
34import { join } from 'node:path' ;
45import { fileURLToPath } from 'node:url' ;
6+ import { run } from './core/exec.ts' ;
57import { VerifyFailure , type Platform } from './core/types.ts' ;
68import { resolveJavaHome , sdkRoot } from './platform/android/sdk.ts' ;
79
@@ -10,14 +12,34 @@ export const IOS_PRODUCT = 'ClerkExpoNativeBuildFixture';
1012export const WORKTREE = fileURLToPath ( new URL ( '../../../../' , import . meta. url ) ) ;
1113export const FIXTURE = join ( WORKTREE , 'integration' , 'templates' , 'expo-native' ) ;
1214
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- ] ;
15+ export type BuildProduct = 'dev-client' | 'standalone' ;
16+
17+ interface Recipe {
18+ readonly configuration : 'Debug' | 'Release' ;
19+ readonly gradle : readonly string [ ] ;
20+ readonly apk : string ;
21+ readonly expoPackages : readonly string [ ] ;
22+ readonly label : string ;
23+ }
24+
25+ const EXPO_PACKAGES = [ 'expo-auth-session' , 'expo-constants' , 'expo-crypto' , 'expo-secure-store' , 'expo-web-browser' ] ;
26+
27+ const RECIPES : Readonly < Record < BuildProduct , Recipe > > = {
28+ 'dev-client' : {
29+ configuration : 'Debug' ,
30+ gradle : [ 'assembleDebug' ] ,
31+ apk : join ( 'debug' , 'app-debug.apk' ) ,
32+ expoPackages : [ ...EXPO_PACKAGES , 'expo-dev-client' ] ,
33+ label : 'dev client' ,
34+ } ,
35+ standalone : {
36+ configuration : 'Release' ,
37+ gradle : [ ':app:createBundleReleaseJsAndAssets' , '--rerun' , 'assembleRelease' ] ,
38+ apk : join ( 'release' , 'app-release.apk' ) ,
39+ expoPackages : EXPO_PACKAGES ,
40+ label : 'JS embedded' ,
41+ } ,
42+ } ;
2143
2244const SHARED_NATIVE_INPUTS = [
2345 'packages/expo/app.plugin.js' ,
@@ -42,14 +64,43 @@ const PLATFORM_NATIVE_INPUTS: Readonly<Record<Platform, readonly string[]>> = {
4264 android : [ 'packages/expo/android' , 'packages/expo-google-signin/android' , 'packages/expo-biometrics/android' ] ,
4365} ;
4466
67+ const BUNDLE_INPUTS = [
68+ '.npmrc' ,
69+ 'package.json' ,
70+ 'pnpm-lock.yaml' ,
71+ 'pnpm-workspace.yaml' ,
72+ 'tsconfig.json' ,
73+ 'turbo.json' ,
74+ 'packages/clerk-js' ,
75+ 'packages/expo' ,
76+ 'packages/expo-biometrics' ,
77+ 'packages/expo-google-signin' ,
78+ 'packages/react' ,
79+ 'packages/shared' ,
80+ 'integration/templates/expo-native' ,
81+ ] as const ;
82+
4583export function nativeInputs ( platform : Platform ) : readonly string [ ] {
4684 return [ ...PLATFORM_NATIVE_INPUTS [ platform ] , ...SHARED_NATIVE_INPUTS ] ;
4785}
4886
49- export function artifact ( platform : Platform ) : string {
87+ export function buildInputs ( platform : Platform , product : BuildProduct ) : readonly string [ ] {
88+ return product === 'standalone' ? [ ...nativeInputs ( platform ) , ...BUNDLE_INPUTS ] : nativeInputs ( platform ) ;
89+ }
90+
91+ export function artifact ( platform : Platform , product : BuildProduct , fixture : string = FIXTURE ) : string {
92+ const recipe = RECIPES [ product ] ;
5093 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' ) ;
94+ ? join (
95+ fixture ,
96+ 'ios' ,
97+ 'build' ,
98+ 'Build' ,
99+ 'Products' ,
100+ `${ recipe . configuration } -iphonesimulator` ,
101+ `${ IOS_PRODUCT } .app` ,
102+ )
103+ : join ( fixture , 'android' , 'app' , 'build' , 'outputs' , 'apk' , recipe . apk ) ;
53104}
54105
55106function step (
@@ -97,14 +148,35 @@ export async function mustStep(
97148
98149export interface FixtureBuild {
99150 readonly platform : Platform ;
151+ readonly product : BuildProduct ;
152+ readonly nativeKey : string ;
100153 readonly buildPackages : boolean ;
101154 readonly progress : ( line : string ) => void ;
102155}
103156
104- async function generateNativeProject ( build : FixtureBuild ) : Promise < void > {
157+ export interface FixtureSite {
158+ readonly worktree : string ;
159+ readonly fixture : string ;
160+ readonly must : typeof mustStep ;
161+ }
162+
163+ const THIS_CHECKOUT : FixtureSite = { worktree : WORKTREE , fixture : FIXTURE , must : mustStep } ;
164+
165+ const nativeProjectMarker = ( site : FixtureSite , platform : Platform ) =>
166+ join ( site . fixture , platform , '.verify-native-project' ) ;
167+
168+ export function nativeProjectIsCurrent ( site : FixtureSite , platform : Platform , wanted : string ) : boolean {
169+ const marker = nativeProjectMarker ( site , platform ) ;
170+ return (
171+ existsSync ( join ( site . fixture , 'node_modules' ) ) && existsSync ( marker ) && readFileSync ( marker , 'utf8' ) === wanted
172+ ) ;
173+ }
174+
175+ async function generateNativeProject ( site : FixtureSite , build : FixtureBuild , wanted : string ) : Promise < void > {
105176 const { platform, progress } = build ;
106- cpSync ( join ( FIXTURE , 'package.sdk-57.json' ) , join ( FIXTURE , 'package.json' ) ) ;
107- await mustStep (
177+ rmSync ( nativeProjectMarker ( site , platform ) , { force : true } ) ;
178+ cpSync ( join ( site . fixture , 'package.sdk-57.json' ) , join ( site . fixture , 'package.json' ) ) ;
179+ await site . must (
108180 'pnpm add the workspace packages' ,
109181 'pnpm' ,
110182 [
@@ -113,21 +185,23 @@ async function generateNativeProject(build: FixtureBuild): Promise<void> {
113185 'link:../../../packages/expo-google-signin' ,
114186 'link:../../../packages/expo-biometrics' ,
115187 ] ,
116- FIXTURE ,
188+ site . fixture ,
117189 ) ;
118- await mustStep ( 'expo install' , 'pnpm' , [ 'expo' , 'install' , ...EXPO_PACKAGES ] , FIXTURE ) ;
190+ await site . must ( 'expo install' , 'pnpm' , [ 'expo' , 'install' , ...RECIPES [ build . product ] . expoPackages ] , site . fixture ) ;
119191 progress ( `build expo prebuild --clean --platform ${ platform } ` ) ;
120- await mustStep ( 'expo prebuild' , 'pnpm' , [ 'expo' , 'prebuild' , '--clean' , '--platform' , platform ] , FIXTURE ) ;
192+ await site . must ( 'expo prebuild' , 'pnpm' , [ 'expo' , 'prebuild' , '--clean' , '--platform' , platform ] , site . fixture ) ;
193+ writeFileSync ( nativeProjectMarker ( site , platform ) , wanted ) ;
121194}
122195
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` ) ;
196+ export async function buildFixture ( build : FixtureBuild , site : FixtureSite = THIS_CHECKOUT ) : Promise < string > {
197+ const { platform, product, progress } = build ;
198+ const recipe = RECIPES [ product ] ;
199+ if ( ! existsSync ( join ( site . worktree , 'node_modules' ) ) ) {
200+ throw new VerifyFailure ( 'NOT_READY' , 'the monorepo has no node_modules' , `cd ${ site . worktree } && pnpm install` ) ;
127201 }
128202 if ( build . buildPackages ) {
129203 progress ( 'build turbo build @clerk/expo, @clerk/expo-biometrics, @clerk/expo-google-signin' ) ;
130- await mustStep (
204+ await site . must (
131205 'turbo build' ,
132206 'pnpm' ,
133207 [
@@ -137,13 +211,18 @@ export async function buildFixture(build: FixtureBuild): Promise<string> {
137211 '--filter=@clerk/expo-biometrics...' ,
138212 '--filter=@clerk/expo-google-signin...' ,
139213 ] ,
140- WORKTREE ,
214+ site . worktree ,
141215 ) ;
142216 }
143- await generateNativeProject ( build ) ;
217+ const wanted = `${ product } ${ build . nativeKey } ` ;
218+ if ( nativeProjectIsCurrent ( site , platform , wanted ) ) {
219+ progress ( `build the ${ platform } project was generated from these native inputs, so expo prebuild is skipped` ) ;
220+ } else {
221+ await generateNativeProject ( site , build , wanted ) ;
222+ }
144223 if ( platform === 'ios' ) {
145- progress ( ' build xcodebuild Debug (dev client)' ) ;
146- await mustStep (
224+ progress ( ` build xcodebuild ${ recipe . configuration } ( ${ recipe . label } )` ) ;
225+ await site . must (
147226 'xcodebuild' ,
148227 'xcodebuild' ,
149228 [
@@ -154,23 +233,58 @@ export async function buildFixture(build: FixtureBuild): Promise<string> {
154233 '-scheme' ,
155234 IOS_PRODUCT ,
156235 '-configuration' ,
157- 'Debug' ,
236+ recipe . configuration ,
158237 '-sdk' ,
159238 'iphonesimulator' ,
160239 '-derivedDataPath' ,
161- join ( FIXTURE , 'ios' , 'build' ) ,
240+ join ( site . fixture , 'ios' , 'build' ) ,
162241 'CODE_SIGN_IDENTITY=-' ,
163242 ] ,
164- FIXTURE ,
243+ site . fixture ,
165244 ) ;
166245 } else {
167246 const java = resolveJavaHome ( ) ;
168247 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 ( ) ,
248+ progress ( `build gradlew ${ recipe . gradle . at ( - 1 ) } (${ recipe . label } )` ) ;
249+ await site . must (
250+ `gradlew ${ recipe . gradle . at ( - 1 ) } ` ,
251+ './gradlew' ,
252+ [ ...recipe . gradle , '-q' ] ,
253+ join ( site . fixture , 'android' ) ,
254+ {
255+ JAVA_HOME : java . home ,
256+ ANDROID_HOME : sdkRoot ( ) ,
257+ } ,
258+ ) ;
259+ }
260+ return artifact ( platform , product , site . fixture ) ;
261+ }
262+
263+ async function committedNativeKey ( platform : Platform ) : Promise < string > {
264+ const listed = await run ( 'git' , [ 'ls-tree' , '-r' , 'HEAD' , '--' , ...nativeInputs ( platform ) ] , { cwd : WORKTREE } ) ;
265+ if ( listed . code !== 0 ) throw new VerifyFailure ( 'NOT_READY' , `git ls-tree failed: ${ listed . stderr . trim ( ) } ` , 'retry' ) ;
266+ return createHash ( 'sha256' ) . update ( listed . stdout ) . digest ( 'hex' ) . slice ( 0 , 12 ) ;
267+ }
268+
269+ if ( import . meta. main ) {
270+ const platform = process . argv [ 2 ] ;
271+ if ( platform !== 'ios' && platform !== 'android' ) {
272+ console . error ( 'usage: fixture.ts ios|android' ) ;
273+ process . exit ( 2 ) ;
274+ }
275+ try {
276+ console . log ( 'build pnpm install --frozen-lockfile' ) ;
277+ await mustStep ( 'pnpm install' , 'pnpm' , [ 'install' , '--frozen-lockfile' ] , WORKTREE ) ;
278+ const built = await buildFixture ( {
279+ platform,
280+ product : 'standalone' ,
281+ nativeKey : await committedNativeKey ( platform ) ,
282+ buildPackages : true ,
283+ progress : line => console . log ( line ) ,
173284 } ) ;
285+ console . log ( `build ${ built } ` ) ;
286+ } catch ( error ) {
287+ console . error ( ( error as Error ) . message ) ;
288+ process . exit ( 1 ) ;
174289 }
175- return artifact ( platform ) ;
176290}
0 commit comments