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 });
6const config_plugins_1 = require("expo/config-plugins");
7const fs_1 = __importDefault(require("fs"));
8const path_1 = __importDefault(require("path"));
9const semver_1 = __importDefault(require("semver"));
10const constants_1 = require("./constants");
11const withDevMenuAppDelegate_1 = require("./withDevMenuAppDelegate");
12const pkg = require('expo-dev-menu/package.json');
13const DEV_MENU_ANDROID_IMPORT = 'expo.modules.devmenu.react.DevMenuAwareReactActivity';
14const DEV_MENU_ACTIVITY_CLASS = 'public class MainActivity extends DevMenuAwareReactActivity {';
15async function readFileAsync(path) {
16    return fs_1.default.promises.readFile(path, 'utf8');
17}
18async function saveFileAsync(path, content) {
19    return fs_1.default.promises.writeFile(path, content, 'utf8');
20}
21function addJavaImports(javaSource, javaImports) {
22    const lines = javaSource.split('\n');
23    const lineIndexWithPackageDeclaration = lines.findIndex((line) => line.match(/^package .*;$/));
24    for (const javaImport of javaImports) {
25        if (!javaSource.includes(javaImport)) {
26            const importStatement = `import ${javaImport};`;
27            lines.splice(lineIndexWithPackageDeclaration + 1, 0, importStatement);
28        }
29    }
30    return lines.join('\n');
31}
32function addLines(content, find, offset, toAdd) {
33    const lines = content.split('\n');
34    let lineIndex = lines.findIndex((line) => line.match(find));
35    for (const newLine of toAdd) {
36        if (!content.includes(newLine)) {
37            lines.splice(lineIndex + offset, 0, newLine);
38            lineIndex++;
39        }
40    }
41    return lines.join('\n');
42}
43async function editPodfile(config, action) {
44    const podfilePath = path_1.default.join(config.modRequest.platformProjectRoot, 'Podfile');
45    try {
46        const podfile = action(await readFileAsync(podfilePath));
47        return await saveFileAsync(podfilePath, podfile);
48    }
49    catch (e) {
50        config_plugins_1.WarningAggregator.addWarningIOS('expo-dev-menu', `Couldn't modified AppDelegate.m - ${e}.
51See the expo-dev-client installation instructions to modify your AppDelegate manually: ${constants_1.InstallationPage}`);
52    }
53}
54const withDevMenuActivity = (config) => {
55    return (0, config_plugins_1.withMainActivity)(config, (config) => {
56        if (config.modResults.language === 'java') {
57            let content = config.modResults.contents;
58            content = addJavaImports(content, [DEV_MENU_ANDROID_IMPORT]);
59            content = content.replace('public class MainActivity extends ReactActivity {', DEV_MENU_ACTIVITY_CLASS);
60            config.modResults.contents = content;
61        }
62        else {
63            config_plugins_1.WarningAggregator.addWarningAndroid('expo-dev-menu', `Cannot automatically configure MainActivity if it's not java.
64See the expo-dev-client installation instructions to modify your MainActivity manually: ${constants_1.InstallationPage}`);
65        }
66        return config;
67    });
68};
69const withDevMenuPodfile = (config) => {
70    return (0, config_plugins_1.withDangerousMod)(config, [
71        'ios',
72        async (config) => {
73            await editPodfile(config, (podfile) => {
74                podfile = podfile.replace("platform :ios, '10.0'", "platform :ios, '11.0'");
75                // Match both variations of Ruby config:
76                // unknown: pod 'expo-dev-menu', path: '../node_modules/expo-dev-menu', :configurations => :debug
77                // Rubocop: pod 'expo-dev-menu', path: '../node_modules/expo-dev-menu', configurations: :debug
78                if (!podfile.match(/pod ['"]expo-dev-menu['"],\s?path: ['"][^'"]*node_modules\/expo-dev-menu['"],\s?:?configurations:?\s(?:=>\s)?:debug/)) {
79                    const packagePath = path_1.default.dirname(require.resolve('expo-dev-menu/package.json'));
80                    const relativePath = path_1.default.relative(config.modRequest.platformProjectRoot, packagePath);
81                    podfile = addLines(podfile, 'use_react_native', 0, [
82                        `  pod 'expo-dev-menu', path: '${relativePath}', :configurations => :debug`,
83                    ]);
84                }
85                return podfile;
86            });
87            return config;
88        },
89    ]);
90};
91const withDevMenu = (config) => {
92    // projects using SDKs before 45 need the old regex-based integration
93    // TODO: remove this config plugin once we drop support for SDK 44
94    if (config.sdkVersion && semver_1.default.lt(config.sdkVersion, '45.0.0')) {
95        config = withDevMenuActivity(config);
96        config = withDevMenuPodfile(config);
97        config = (0, withDevMenuAppDelegate_1.withDevMenuAppDelegate)(config);
98    }
99    return config;
100};
101exports.default = (0, config_plugins_1.createRunOncePlugin)(withDevMenu, pkg.name, pkg.version);
102