1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.MATCH_INIT = void 0;
7exports.addGoogleMapsAppDelegateImport = addGoogleMapsAppDelegateImport;
8exports.addGoogleMapsAppDelegateInit = addGoogleMapsAppDelegateInit;
9exports.addMapsCocoaPods = addMapsCocoaPods;
10exports.getGoogleMapsApiKey = getGoogleMapsApiKey;
11exports.removeGoogleMapsAppDelegateImport = removeGoogleMapsAppDelegateImport;
12exports.removeGoogleMapsAppDelegateInit = removeGoogleMapsAppDelegateInit;
13exports.removeMapsCocoaPods = removeMapsCocoaPods;
14exports.setGoogleMapsApiKey = setGoogleMapsApiKey;
15exports.withMaps = void 0;
16function _fs() {
17  const data = _interopRequireDefault(require("fs"));
18  _fs = function () {
19    return data;
20  };
21  return data;
22}
23function _path() {
24  const data = _interopRequireDefault(require("path"));
25  _path = function () {
26    return data;
27  };
28  return data;
29}
30function _resolveFrom() {
31  const data = _interopRequireDefault(require("resolve-from"));
32  _resolveFrom = function () {
33    return data;
34  };
35  return data;
36}
37function _iosPlugins() {
38  const data = require("../plugins/ios-plugins");
39  _iosPlugins = function () {
40    return data;
41  };
42  return data;
43}
44function _withDangerousMod() {
45  const data = require("../plugins/withDangerousMod");
46  _withDangerousMod = function () {
47    return data;
48  };
49  return data;
50}
51function _generateCode() {
52  const data = require("../utils/generateCode");
53  _generateCode = function () {
54    return data;
55  };
56  return data;
57}
58function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59const debug = require('debug')('expo:config-plugins:ios:maps');
60
61// Match against `UMModuleRegistryAdapter` (unimodules), and React Native without unimodules (Expo Modules), and SDK +44 React AppDelegate subscriber.
62const MATCH_INIT = /(?:(self\.|_)(\w+)\s?=\s?\[\[UMModuleRegistryAdapter alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[\[RCTBridge alloc\])|(\[self\.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions\])/g;
63exports.MATCH_INIT = MATCH_INIT;
64const withGoogleMapsKey = (0, _iosPlugins().createInfoPlistPlugin)(setGoogleMapsApiKey, 'withGoogleMapsKey');
65const withMaps = config => {
66  config = withGoogleMapsKey(config);
67  const apiKey = getGoogleMapsApiKey(config);
68  // Technically adds react-native-maps (Apple maps) and google maps.
69
70  debug('Google Maps API Key:', apiKey);
71  config = withMapsCocoaPods(config, {
72    useGoogleMaps: !!apiKey
73  });
74
75  // Adds/Removes AppDelegate setup for Google Maps API on iOS
76  config = withGoogleMapsAppDelegate(config, {
77    apiKey
78  });
79  return config;
80};
81exports.withMaps = withMaps;
82function getGoogleMapsApiKey(config) {
83  var _config$ios$config$go, _config$ios, _config$ios$config;
84  return (_config$ios$config$go = (_config$ios = config.ios) === null || _config$ios === void 0 ? void 0 : (_config$ios$config = _config$ios.config) === null || _config$ios$config === void 0 ? void 0 : _config$ios$config.googleMapsApiKey) !== null && _config$ios$config$go !== void 0 ? _config$ios$config$go : null;
85}
86function setGoogleMapsApiKey(config, {
87  GMSApiKey,
88  ...infoPlist
89}) {
90  const apiKey = getGoogleMapsApiKey(config);
91  if (apiKey === null) {
92    return infoPlist;
93  }
94  return {
95    ...infoPlist,
96    GMSApiKey: apiKey
97  };
98}
99function addGoogleMapsAppDelegateImport(src) {
100  const newSrc = [];
101  newSrc.push('#if __has_include(<GoogleMaps/GoogleMaps.h>)', '#import <GoogleMaps/GoogleMaps.h>', '#endif');
102  return (0, _generateCode().mergeContents)({
103    tag: 'react-native-maps-import',
104    src,
105    newSrc: newSrc.join('\n'),
106    anchor: /#import "AppDelegate\.h"/,
107    offset: 1,
108    comment: '//'
109  });
110}
111function removeGoogleMapsAppDelegateImport(src) {
112  return (0, _generateCode().removeContents)({
113    tag: 'react-native-maps-import',
114    src
115  });
116}
117function addGoogleMapsAppDelegateInit(src, apiKey) {
118  const newSrc = [];
119  newSrc.push('#if __has_include(<GoogleMaps/GoogleMaps.h>)', `  [GMSServices provideAPIKey:@"${apiKey}"];`, '#endif');
120  return (0, _generateCode().mergeContents)({
121    tag: 'react-native-maps-init',
122    src,
123    newSrc: newSrc.join('\n'),
124    anchor: MATCH_INIT,
125    offset: 0,
126    comment: '//'
127  });
128}
129function removeGoogleMapsAppDelegateInit(src) {
130  return (0, _generateCode().removeContents)({
131    tag: 'react-native-maps-init',
132    src
133  });
134}
135
136/**
137 * @param src The contents of the Podfile.
138 * @returns Podfile with Google Maps added.
139 */
140function addMapsCocoaPods(src) {
141  return (0, _generateCode().mergeContents)({
142    tag: 'react-native-maps',
143    src,
144    newSrc: `  pod 'react-native-google-maps', path: File.dirname(\`node --print "require.resolve('react-native-maps/package.json')"\`)`,
145    anchor: /use_native_modules/,
146    offset: 0,
147    comment: '#'
148  });
149}
150function removeMapsCocoaPods(src) {
151  return (0, _generateCode().removeContents)({
152    tag: 'react-native-maps',
153    src
154  });
155}
156function isReactNativeMapsInstalled(projectRoot) {
157  const resolved = _resolveFrom().default.silent(projectRoot, 'react-native-maps/package.json');
158  return resolved ? _path().default.dirname(resolved) : null;
159}
160function isReactNativeMapsAutolinked(config) {
161  // Only add the native code changes if we know that the package is going to be linked natively.
162  // This is specifically for monorepo support where one app might have react-native-maps (adding it to the node_modules)
163  // but another app will not have it installed in the package.json, causing it to not be linked natively.
164  // This workaround only exists because react-native-maps doesn't have a config plugin vendored in the package.
165
166  // TODO: `react-native-maps` doesn't use Expo autolinking so we cannot safely disable the module.
167  return true;
168
169  // return (
170  //   !config._internal?.autolinkedModules ||
171  //   config._internal.autolinkedModules.includes('react-native-maps')
172  // );
173}
174
175const withMapsCocoaPods = (config, {
176  useGoogleMaps
177}) => {
178  return (0, _withDangerousMod().withDangerousMod)(config, ['ios', async config => {
179    const filePath = _path().default.join(config.modRequest.platformProjectRoot, 'Podfile');
180    const contents = await _fs().default.promises.readFile(filePath, 'utf-8');
181    let results;
182    // Only add the block if react-native-maps is installed in the project (best effort).
183    // Generally prebuild runs after a yarn install so this should always work as expected.
184    const googleMapsPath = isReactNativeMapsInstalled(config.modRequest.projectRoot);
185    const isLinked = isReactNativeMapsAutolinked(config);
186    debug('Is Expo Autolinked:', isLinked);
187    debug('react-native-maps path:', googleMapsPath);
188    if (isLinked && googleMapsPath && useGoogleMaps) {
189      try {
190        results = addMapsCocoaPods(contents);
191      } catch (error) {
192        if (error.code === 'ERR_NO_MATCH') {
193          throw new Error(`Cannot add react-native-maps to the project's ios/Podfile because it's malformed. Please report this with a copy of your project Podfile.`);
194        }
195        throw error;
196      }
197    } else {
198      // If the package is no longer installed, then remove the block.
199      results = removeMapsCocoaPods(contents);
200    }
201    if (results.didMerge || results.didClear) {
202      await _fs().default.promises.writeFile(filePath, results.contents);
203    }
204    return config;
205  }]);
206};
207const withGoogleMapsAppDelegate = (config, {
208  apiKey
209}) => {
210  return (0, _iosPlugins().withAppDelegate)(config, config => {
211    if (['objc', 'objcpp'].includes(config.modResults.language)) {
212      if (apiKey && isReactNativeMapsAutolinked(config) && isReactNativeMapsInstalled(config.modRequest.projectRoot)) {
213        try {
214          config.modResults.contents = addGoogleMapsAppDelegateImport(config.modResults.contents).contents;
215          config.modResults.contents = addGoogleMapsAppDelegateInit(config.modResults.contents, apiKey).contents;
216        } catch (error) {
217          if (error.code === 'ERR_NO_MATCH') {
218            throw new Error(`Cannot add Google Maps to the project's AppDelegate because it's malformed. Please report this with a copy of your project AppDelegate.`);
219          }
220          throw error;
221        }
222      } else {
223        config.modResults.contents = removeGoogleMapsAppDelegateImport(config.modResults.contents).contents;
224        config.modResults.contents = removeGoogleMapsAppDelegateInit(config.modResults.contents).contents;
225      }
226    } else {
227      throw new Error(`Cannot setup Google Maps because the project AppDelegate is not a supported language: ${config.modResults.language}`);
228    }
229    return config;
230  });
231};
232//# sourceMappingURL=Maps.js.map