This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.

175 lines
7.5 KiB
JavaScript
Raw Normal View History

2021-04-02 02:24:13 +03:00
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_extra_1 = __importDefault(require("fs-extra"));
const path_1 = __importDefault(require("path"));
const core_plugins_1 = require("../plugins/core-plugins");
const ios_plugins_1 = require("../plugins/ios-plugins");
const generateCode_1 = require("../utils/generateCode");
const resolvePackageRootFolder_1 = require("../utils/resolvePackageRootFolder");
exports.MATCH_INIT = /(?:(self\.|_)(\w+)\s?=\s?\[\[UMModuleRegistryAdapter alloc\])|(?:RCTBridge\s?\*\s?(\w+)\s?=\s?\[\[RCTBridge alloc\])/g;
const withGoogleMapsKey = ios_plugins_1.createInfoPlistPlugin(setGoogleMapsApiKey, 'withGoogleMapsKey');
exports.withMaps = config => {
config = withGoogleMapsKey(config);
const apiKey = getGoogleMapsApiKey(config);
// Technically adds react-native-maps (Apple maps) and google maps.
config = withMapsCocoaPods(config, { useGoogleMaps: !!apiKey });
// Adds/Removes AppDelegate setup for Google Maps API on iOS
config = withGoogleMapsAppDelegate(config, { apiKey });
return config;
};
function getGoogleMapsApiKey(config) {
var _a, _b, _c;
return (_c = (_b = (_a = config.ios) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.googleMapsApiKey) !== null && _c !== void 0 ? _c : null;
}
exports.getGoogleMapsApiKey = getGoogleMapsApiKey;
function setGoogleMapsApiKey(config, _a) {
var { GMSApiKey } = _a, infoPlist = __rest(_a, ["GMSApiKey"]);
const apiKey = getGoogleMapsApiKey(config);
if (apiKey === null) {
return infoPlist;
}
return Object.assign(Object.assign({}, infoPlist), { GMSApiKey: apiKey });
}
exports.setGoogleMapsApiKey = setGoogleMapsApiKey;
function addGoogleMapsAppDelegateImport(src) {
const newSrc = [];
newSrc.push('#if __has_include(<GoogleMaps/GoogleMaps.h>)', '#import <GoogleMaps/GoogleMaps.h>', '#endif');
return generateCode_1.mergeContents({
tag: 'react-native-maps-import',
src,
newSrc: newSrc.join('\n'),
anchor: /#import "AppDelegate\.h"/,
offset: 1,
comment: '//',
});
}
exports.addGoogleMapsAppDelegateImport = addGoogleMapsAppDelegateImport;
function removeGoogleMapsAppDelegateImport(src) {
return generateCode_1.removeContents({
tag: 'react-native-maps-import',
src,
});
}
exports.removeGoogleMapsAppDelegateImport = removeGoogleMapsAppDelegateImport;
function addGoogleMapsAppDelegateInit(src, apiKey) {
const newSrc = [];
newSrc.push('#if __has_include(<GoogleMaps/GoogleMaps.h>)', ` [GMSServices provideAPIKey:@"${apiKey}"];`, '#endif');
return generateCode_1.mergeContents({
tag: 'react-native-maps-init',
src,
newSrc: newSrc.join('\n'),
anchor: exports.MATCH_INIT,
offset: 0,
comment: '//',
});
}
exports.addGoogleMapsAppDelegateInit = addGoogleMapsAppDelegateInit;
function removeGoogleMapsAppDelegateInit(src) {
return generateCode_1.removeContents({
tag: 'react-native-maps-init',
src,
});
}
exports.removeGoogleMapsAppDelegateInit = removeGoogleMapsAppDelegateInit;
/**
* @param src
* @param useGoogleMaps
* @param googleMapsPath '../node_modules/react-native-maps'
* @returns
*/
function addMapsCocoaPods(src, googleMapsPath) {
return generateCode_1.mergeContents({
tag: 'react-native-maps',
src,
newSrc: ` pod 'react-native-google-maps', path: '${googleMapsPath}'`,
anchor: /use_native_modules/,
offset: 0,
comment: '#',
});
}
exports.addMapsCocoaPods = addMapsCocoaPods;
function removeMapsCocoaPods(src) {
return generateCode_1.removeContents({
tag: 'react-native-maps',
src,
});
}
exports.removeMapsCocoaPods = removeMapsCocoaPods;
function isReactNativeMapsInstalled(projectRoot) {
return resolvePackageRootFolder_1.resolvePackageRootFolder(projectRoot, 'react-native-maps');
}
const withMapsCocoaPods = (config, { useGoogleMaps }) => {
return core_plugins_1.withDangerousMod(config, [
'ios',
async (config) => {
const filePath = path_1.default.join(config.modRequest.platformProjectRoot, 'Podfile');
const contents = await fs_extra_1.default.readFile(filePath, 'utf-8');
let results;
// Only add the block if react-native-maps is installed in the project (best effort).
// Generally prebuild runs after a yarn install so this should always work as expected.
const googleMapsPath = isReactNativeMapsInstalled(config.modRequest.projectRoot);
if (googleMapsPath && useGoogleMaps) {
// Make the pod path relative to the ios folder.
const googleMapsPodPath = path_1.default.relative(config.modRequest.platformProjectRoot, googleMapsPath);
try {
results = addMapsCocoaPods(contents, googleMapsPodPath);
}
catch (error) {
if (error.code === 'ERR_NO_MATCH') {
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.`);
}
throw error;
}
}
else {
// If the package is no longer installed, then remove the block.
results = removeMapsCocoaPods(contents);
}
if (results.didMerge || results.didClear) {
await fs_extra_1.default.writeFile(filePath, results.contents);
}
return config;
},
]);
};
const withGoogleMapsAppDelegate = (config, { apiKey }) => {
return ios_plugins_1.withAppDelegate(config, config => {
if (config.modResults.language === 'objc') {
if (apiKey && isReactNativeMapsInstalled(config.modRequest.projectRoot)) {
try {
config.modResults.contents = addGoogleMapsAppDelegateImport(config.modResults.contents).contents;
config.modResults.contents = addGoogleMapsAppDelegateInit(config.modResults.contents, apiKey).contents;
}
catch (error) {
if (error.code === 'ERR_NO_MATCH') {
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.`);
}
throw error;
}
}
else {
config.modResults.contents = removeGoogleMapsAppDelegateImport(config.modResults.contents).contents;
config.modResults.contents = removeGoogleMapsAppDelegateInit(config.modResults.contents).contents;
}
}
else {
throw new Error('Cannot setup Google Maps because the AppDelegate is not Objective C');
}
return config;
});
};
//# sourceMappingURL=Maps.js.map