import { Platform } from '@expo/eas-build-job' ; import envString from 'env-string' ; import { MissingParentProfileError , MissingProfileError } from '../errors' ; import { EasJson } from '../types' ; import { AndroidSubmitProfileSchema , IosSubmitProfileSchema } from './schema' ; import { AndroidSubmitProfileFieldsToEvaluate , IosSubmitProfileFieldsToEvaluate , SubmitProfile , } from './types' ; export function resolveSubmitProfile < T extends Platform > ( { easJson , platform , profileName , } ) { const submitProfile = resolveProfile ( { easJson , platform , profileName , } ) ; const unevaluatedProfile = mergeProfiles ( getDefaultProfile ( platform ) , submitProfile ) ; return evaluateFields ( platform , unevaluatedProfile ) ; } function resolveProfile < T extends Platform > ( { easJson , profileName , depth = 0 , platform , } ) { if ( depth >= 2 ) { throw new Error ( 'Too long chain of profile extensions, make sure "extends" keys do not make a cycle' ) ; } const profile = easJson . submit ?. [ profileName ] ; if ( ! profile ) { if ( depth === 0 ) { throw new MissingProfileError ( `Missing submit profile in eas.json: ${ profileName }` ) ; } else { throw new MissingParentProfileError ( `Extending non-existent submit profile in eas.json: ${ profileName }` ) ; } } const { extends : baseProfileName , ... rest } = profile ; const platformProfile = rest [ platform ] as SubmitProfile < T > | undefined ; if ( baseProfileName ) { const baseProfile = resolveProfile ( { easJson , platform , profileName : baseProfileName , depth : depth + 1 , } ) ; return mergeProfiles ( baseProfile , platformProfile ) ; } else { return platformProfile ; } } function mergeProfiles < T extends Platform > ( base , update ? ) ; function mergeProfiles < T extends Platform > ( base ? , update ? ) ; function mergeProfiles < T extends Platform > ( base ? , update ? ) { if ( ! update ) { return base ; } return { ... base , ... update } ; } export function getDefaultProfile < T extends Platform > ( platform ) { const Schema = platform === Platform . ANDROID ? AndroidSubmitProfileSchema : IosSubmitProfileSchema ; return Schema . validate ( { } , { allowUnknown : false , abortEarly : false , convert : true } ) . value ; } function evaluateFields < T extends Platform > ( platform , profile ) { const fields = platform === Platform . ANDROID ? AndroidSubmitProfileFieldsToEvaluate : IosSubmitProfileFieldsToEvaluate ; const evaluatedProfile = { ... profile } ; for ( const field of fields ) { if ( field in evaluatedProfile ) { evaluatedProfile [ field ] = envString ( evaluatedProfile [ field ] , process . env ) ; } } return evaluatedProfile ; }