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