1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.validateConfig = void 0;
7const ajv_1 = __importDefault(require("ajv"));
8const semver_1 = __importDefault(require("semver"));
9/**
10 * The minimal supported versions. These values should align to SDK.
11 * @ignore
12 */
13const EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS = {
14    android: {
15        minSdkVersion: 21,
16        compileSdkVersion: 31,
17        targetSdkVersion: 31,
18        kotlinVersion: '1.6.10',
19    },
20    ios: {
21        deploymentTarget: '13.0',
22    },
23};
24const schema = {
25    type: 'object',
26    properties: {
27        android: {
28            type: 'object',
29            properties: {
30                newArchEnabled: { type: 'boolean', nullable: true },
31                minSdkVersion: { type: 'integer', nullable: true },
32                compileSdkVersion: { type: 'integer', nullable: true },
33                targetSdkVersion: { type: 'integer', nullable: true },
34                buildToolsVersion: { type: 'string', nullable: true },
35                kotlinVersion: { type: 'string', nullable: true },
36                enableProguardInReleaseBuilds: { type: 'boolean', nullable: true },
37                enableShrinkResourcesInReleaseBuilds: { type: 'boolean', nullable: true },
38                extraProguardRules: { type: 'string', nullable: true },
39                flipper: {
40                    type: 'string',
41                    nullable: true,
42                },
43                packagingOptions: {
44                    type: 'object',
45                    properties: {
46                        pickFirst: { type: 'array', items: { type: 'string' }, nullable: true },
47                        exclude: { type: 'array', items: { type: 'string' }, nullable: true },
48                        merge: { type: 'array', items: { type: 'string' }, nullable: true },
49                        doNotStrip: { type: 'array', items: { type: 'string' }, nullable: true },
50                    },
51                    nullable: true,
52                },
53                networkInspector: { type: 'boolean', nullable: true },
54                extraMavenRepos: { type: 'array', items: { type: 'string' }, nullable: true },
55                usesCleartextTraffic: { type: 'boolean', nullable: true },
56            },
57            nullable: true,
58        },
59        ios: {
60            type: 'object',
61            properties: {
62                newArchEnabled: { type: 'boolean', nullable: true },
63                deploymentTarget: { type: 'string', pattern: '\\d+\\.\\d+', nullable: true },
64                useFrameworks: { type: 'string', enum: ['static', 'dynamic'], nullable: true },
65                flipper: {
66                    type: ['boolean', 'string'],
67                    nullable: true,
68                },
69                networkInspector: { type: 'boolean', nullable: true },
70                extraPods: {
71                    type: 'array',
72                    items: {
73                        type: 'object',
74                        required: ['name'],
75                        properties: {
76                            name: { type: 'string' },
77                            version: { type: 'string', nullable: true },
78                            configurations: { type: 'array', items: { type: 'string' }, nullable: true },
79                            modular_headers: { type: 'boolean', nullable: true },
80                            source: { type: 'string', nullable: true },
81                            path: { type: 'string', nullable: true },
82                            podspec: { type: 'string', nullable: true },
83                            testspecs: { type: 'array', items: { type: 'string' }, nullable: true },
84                            git: { type: 'string', nullable: true },
85                            branch: { type: 'string', nullable: true },
86                            tag: { type: 'string', nullable: true },
87                            commit: { type: 'string', nullable: true },
88                        },
89                    },
90                    nullable: true,
91                },
92            },
93            nullable: true,
94        },
95    },
96};
97// note(Kudo): For the implementation, we check items one by one because Ajv does not well support custom error message.
98/**
99 * Checks if specified versions meets Expo minimal supported versions.
100 * Will throw error message whenever there are invalid versions.
101 *
102 * @param config The validated config passed from Ajv.
103 * @ignore
104 */
105function maybeThrowInvalidVersions(config) {
106    const checkItems = [
107        {
108            name: 'android.minSdkVersion',
109            configVersion: config.android?.minSdkVersion,
110            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.minSdkVersion,
111        },
112        {
113            name: 'android.compileSdkVersion',
114            configVersion: config.android?.compileSdkVersion,
115            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.compileSdkVersion,
116        },
117        {
118            name: 'android.targetSdkVersion',
119            configVersion: config.android?.targetSdkVersion,
120            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.targetSdkVersion,
121        },
122        {
123            name: 'android.kotlinVersion',
124            configVersion: config.android?.kotlinVersion,
125            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.kotlinVersion,
126        },
127        {
128            name: 'ios.deploymentTarget',
129            configVersion: config.ios?.deploymentTarget,
130            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.ios.deploymentTarget,
131        },
132    ];
133    for (const { name, configVersion, minimalVersion } of checkItems) {
134        if (typeof configVersion === 'number' &&
135            typeof minimalVersion === 'number' &&
136            configVersion < minimalVersion) {
137            throw new Error(`\`${name}\` needs to be at least version ${minimalVersion}.`);
138        }
139        if (typeof configVersion === 'string' &&
140            typeof minimalVersion === 'string' &&
141            semver_1.default.lt(semver_1.default.coerce(configVersion) ?? '0.0.0', semver_1.default.coerce(minimalVersion) ?? '0.0.0')) {
142            throw new Error(`\`${name}\` needs to be at least version ${minimalVersion}.`);
143        }
144    }
145}
146/**
147 * @ignore
148 */
149function validateConfig(config) {
150    const validate = new ajv_1.default({ allowUnionTypes: true }).compile(schema);
151    if (!validate(config)) {
152        throw new Error('Invalid expo-build-properties config: ' + JSON.stringify(validate.errors));
153    }
154    maybeThrowInvalidVersions(config);
155    // explicitly block using use_frameworks and Flipper in iOS
156    // https://github.com/facebook/flipper/issues/2414
157    if (Boolean(config.ios?.flipper) && config.ios?.useFrameworks !== undefined) {
158        throw new Error('`ios.flipper` cannot be enabled when `ios.useFrameworks` is set.');
159    }
160    if (config.android?.enableShrinkResourcesInReleaseBuilds === true &&
161        config.android?.enableProguardInReleaseBuilds !== true) {
162        throw new Error('`android.enableShrinkResourcesInReleaseBuilds` requires `android.enableProguardInReleaseBuilds` to be enabled.');
163    }
164    return config;
165}
166exports.validateConfig = validateConfig;
167