UNPKG

4.37 kBJavaScriptView Raw
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 fs_extra_1 = __importDefault(require("fs-extra"));
7const glob_1 = require("glob");
8const path_1 = require("path");
9// @ts-ignore
10const xcode_1 = require("xcode");
11const plist_1 = __importDefault(require("@expo/plist"));
12function getBundleIdentifier(config) {
13 return config.ios && config.ios.bundleIdentifier ? config.ios.bundleIdentifier : null;
14}
15exports.getBundleIdentifier = getBundleIdentifier;
16/**
17 * In Turtle v1 we set the bundleIdentifier directly on Info.plist rather
18 * than in pbxproj
19 */
20function setBundleIdentifier(config, infoPlist) {
21 let bundleIdentifier = getBundleIdentifier(config);
22 if (!bundleIdentifier) {
23 return infoPlist;
24 }
25 return Object.assign(Object.assign({}, infoPlist), { CFBundleIdentifier: bundleIdentifier });
26}
27exports.setBundleIdentifier = setBundleIdentifier;
28/**
29 * Updates the bundle identifier for a given pbxproj
30 * * @param pbxprojPath Path to pbxproj file
31 * @param bundleIdentifier Bundle identifier to set in the pbxproj
32 */
33function updateBundleIdentifierForPbxproj(pbxprojPath, bundleIdentifier) {
34 const project = xcode_1.project(pbxprojPath);
35 project.parseSync();
36 Object.entries(project.pbxXCBuildConfigurationSection())
37 .filter(filterComments)
38 .filter(filterConfig)
39 .filter(filterHosts)
40 .forEach(({ 1: { buildSettings } }) => {
41 var _a;
42 if (buildSettings.PRODUCT_BUNDLE_IDENTIFIER === bundleIdentifier) {
43 return;
44 }
45 buildSettings.PRODUCT_BUNDLE_IDENTIFIER = `"${bundleIdentifier}"`;
46 const productName = bundleIdentifier.split('.').pop();
47 if (!((_a = productName) === null || _a === void 0 ? void 0 : _a.includes('$'))) {
48 buildSettings.PRODUCT_NAME = productName;
49 }
50 });
51 fs_extra_1.default.writeFileSync(pbxprojPath, project.writeSync());
52}
53exports.updateBundleIdentifierForPbxproj = updateBundleIdentifierForPbxproj;
54/**
55 * Updates the bundle identifier for pbx projects inside the ios directory of the given project root
56 *
57 * @param projectRoot Path to project root containing the ios directory
58 * @param bundleIdentifier Desired bundle identifier
59 */
60function setBundleIdentifierForPbxproj(projectRoot, bundleIdentifier) {
61 // Get all pbx projects in the ${projectRoot}/ios directory
62 const pbxprojPaths = glob_1.sync(path_1.join(projectRoot, 'ios', '*', 'project.pbxproj'));
63 for (const pbxprojPath of pbxprojPaths) {
64 updateBundleIdentifierForPbxproj(pbxprojPath, bundleIdentifier);
65 }
66}
67exports.setBundleIdentifierForPbxproj = setBundleIdentifierForPbxproj;
68/**
69 * Reset bundle identifier field in Info.plist to use PRODUCT_BUNDLE_IDENTIFIER, as recommended by Apple.
70 */
71const defaultBundleId = '$(PRODUCT_BUNDLE_IDENTIFIER)';
72function resetAllPlistBundleIdentifiers(projectRoot) {
73 const infoPlistPaths = glob_1.sync(path_1.join(projectRoot, 'ios', '*', 'Info.plist'));
74 for (const plistPath of infoPlistPaths) {
75 resetPlistBundleIdentifier(plistPath);
76 }
77}
78exports.resetAllPlistBundleIdentifiers = resetAllPlistBundleIdentifiers;
79function resetPlistBundleIdentifier(plistPath) {
80 const rawPlist = fs_extra_1.default.readFileSync(plistPath, 'utf8');
81 const plistObject = plist_1.default.parse(rawPlist);
82 if (plistObject.CFBundleIdentifier) {
83 if (plistObject.CFBundleIdentifier === defaultBundleId)
84 return;
85 // attempt to match default Info.plist format
86 const format = { pretty: true, indent: `\t` };
87 const xml = plist_1.default.build(Object.assign(Object.assign({}, plistObject), { CFBundleIdentifier: defaultBundleId }), format);
88 if (xml !== rawPlist) {
89 fs_extra_1.default.writeFileSync(plistPath, xml);
90 }
91 }
92}
93exports.resetPlistBundleIdentifier = resetPlistBundleIdentifier;
94function filterComments([item]) {
95 return !item.endsWith(`_comment`);
96}
97function filterConfig(input) {
98 const { 1: { isa }, } = input;
99 return isa === 'XCBuildConfiguration';
100}
101function filterHosts(input) {
102 const { 1: { buildSettings }, } = input;
103 return !buildSettings.TEST_HOST;
104}
105//# sourceMappingURL=BundleIdentifier.js.map
\No newline at end of file