@armanatz/expo-hms-location
Version:
Expo config plugin to configure @hmscore/react-native-hms-location on prebuild
49 lines (48 loc) • 2.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports._addAppIdToManifest = _addAppIdToManifest;
exports.default = withManifestAppId;
const config_plugins_1 = require("expo/config-plugins");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = config_plugins_1.AndroidConfig.Manifest;
async function _addAppIdToManifest(androidManifest, projectRoot, agConnectServicesFile) {
let filePathToRead = agConnectServicesFile;
if (agConnectServicesFile.startsWith('.', 0)) {
filePathToRead = path_1.default.join(projectRoot, agConnectServicesFile);
}
const mainApplication = getMainApplicationOrThrow(androidManifest);
try {
const fileContents = await fs_1.default.promises.readFile(filePathToRead, 'utf-8');
const parsedAGConnectServices = JSON.parse(fileContents);
if (parsedAGConnectServices &&
parsedAGConnectServices.app_info &&
parsedAGConnectServices.app_info.app_id) {
addMetaDataItemToMainApplication(mainApplication, 'com.huawei.hms.client.appid', `appid=${parsedAGConnectServices.app_info.app_id}`);
}
else {
throw new Error(`Could not find app_info.app_id in the agconnect-services.json file at ${filePathToRead}`);
}
}
catch (error) {
if (error instanceof SyntaxError) {
throw new Error(`withHMSLocation: Could not parse the agconnect-services.json file at ${filePathToRead}`);
}
if (error instanceof Error) {
if (error.message.includes('no such file or directory')) {
throw new Error(`withHMSLocation: Could not read the agconnect-services.json file at ${filePathToRead}`);
}
throw new Error(`withHMSLocation: ${error.message}`);
}
}
return androidManifest;
}
function withManifestAppId(config, agConnectServicesFile) {
return (0, config_plugins_1.withAndroidManifest)(config, async ({ modResults, modRequest, ...exportedConfig }) => {
modResults = await _addAppIdToManifest(modResults, modRequest.projectRoot, agConnectServicesFile);
return { modResults, modRequest, ...exportedConfig };
});
}