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            },
54            nullable: true,
55        },
56        ios: {
57            type: 'object',
58            properties: {
59                newArchEnabled: { type: 'boolean', nullable: true },
60                deploymentTarget: { type: 'string', pattern: '\\d+\\.\\d+', nullable: true },
61                useFrameworks: { type: 'string', enum: ['static', 'dynamic'], nullable: true },
62                flipper: {
63                    type: ['boolean', 'string'],
64                    nullable: true,
65                },
66            },
67            nullable: true,
68        },
69    },
70};
71// note(Kudo): For the implementation, we check items one by one because Ajv does not well support custom error message.
72/**
73 * Checks if specified versions meets Expo minimal supported versions.
74 * Will throw error message whenever there are invalid versions.
75 *
76 * @param config The validated config passed from Ajv.
77 * @ignore
78 */
79function maybeThrowInvalidVersions(config) {
80    const checkItems = [
81        {
82            name: 'android.minSdkVersion',
83            configVersion: config.android?.minSdkVersion,
84            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.minSdkVersion,
85        },
86        {
87            name: 'android.compileSdkVersion',
88            configVersion: config.android?.compileSdkVersion,
89            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.compileSdkVersion,
90        },
91        {
92            name: 'android.targetSdkVersion',
93            configVersion: config.android?.targetSdkVersion,
94            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.targetSdkVersion,
95        },
96        {
97            name: 'android.kotlinVersion',
98            configVersion: config.android?.kotlinVersion,
99            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.android.kotlinVersion,
100        },
101        {
102            name: 'ios.deploymentTarget',
103            configVersion: config.ios?.deploymentTarget,
104            minimalVersion: EXPO_SDK_MINIMAL_SUPPORTED_VERSIONS.ios.deploymentTarget,
105        },
106    ];
107    for (const { name, configVersion, minimalVersion } of checkItems) {
108        if (typeof configVersion === 'number' &&
109            typeof minimalVersion === 'number' &&
110            configVersion < minimalVersion) {
111            throw new Error(`\`${name}\` needs to be at least version ${minimalVersion}.`);
112        }
113        if (typeof configVersion === 'string' &&
114            typeof minimalVersion === 'string' &&
115            semver_1.default.lt(semver_1.default.coerce(configVersion) ?? '0.0.0', semver_1.default.coerce(minimalVersion) ?? '0.0.0')) {
116            throw new Error(`\`${name}\` needs to be at least version ${minimalVersion}.`);
117        }
118    }
119}
120/**
121 * @ignore
122 */
123function validateConfig(config) {
124    const validate = new ajv_1.default({ allowUnionTypes: true }).compile(schema);
125    if (!validate(config)) {
126        throw new Error('Invalid expo-build-properties config: ' + JSON.stringify(validate.errors));
127    }
128    maybeThrowInvalidVersions(config);
129    // explicitly block using use_frameworks and Flipper in iOS
130    // https://github.com/facebook/flipper/issues/2414
131    if (config?.ios?.flipper !== undefined && config?.ios?.useFrameworks !== undefined) {
132        throw new Error('`ios.flipper` cannot be enabled when `ios.useFrameworks` is set.');
133    }
134    if (config.android?.enableShrinkResourcesInReleaseBuilds === true &&
135        config.android?.enableProguardInReleaseBuilds !== true) {
136        throw new Error('`android.enableShrinkResourcesInReleaseBuilds` requires `android.enableProguardInReleaseBuilds` to be enabled.');
137    }
138    return config;
139}
140exports.validateConfig = validateConfig;
141