Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

withShareExtensionDisplayName.cjs

89 lines3.1 KB

1"use strict";
2​
3// Brand the extension while leaving its initially-empty signing team intact: Expo uses that state
4// to select ios.appleTeamId and enable first-time profile provisioning. Expo runs same-type mods
5// in reverse registration order, so this executes after the extension exists.
6​
7const fs = require("fs");
8const path = require("path");
9const { withDangerousMod, withXcodeProject } = require("expo/config-plugins");
10​
11const TARGET_NAME = "expo-sharing-extension";
12​
13function stripComments(section) {
14 return Object.fromEntries(
15 Object.entries(section ?? {}).filter(([key]) => !key.endsWith("_comment")),
16 );
18​
19function findByName(section, name) {
20 return Object.entries(stripComments(section)).find(([, value]) => value?.name === name) ?? null;
22​
23function escapeXml(value) {
24 return value
25 .replaceAll("&", "&")
26 .replaceAll("<", "&lt;")
27 .replaceAll(">", "&gt;")
28 .replaceAll('"', "&quot;")
29 .replaceAll("'", "&apos;");
31​
32function withDisplayNamePlist(config, displayName) {
33 return withDangerousMod(config, [
34 "ios",
35 (cfg) => {
36 const plistPath = path.join(cfg.modRequest.platformProjectRoot, TARGET_NAME, "Info.plist");
37 if (!fs.existsSync(plistPath)) {
38 throw new Error(
39 `${TARGET_NAME}/Info.plist was not generated. Register withShareExtensionDisplayName before expo-sharing.`,
40 );
41 }
42 const source = fs.readFileSync(plistPath, "utf8");
43 const displayNamePattern = /(<key>CFBundleDisplayName<\/key>\s*<string>)[^<]*(<\/string>)/;
44 if (!displayNamePattern.test(source)) {
45 throw new Error(`Could not update CFBundleDisplayName in ${plistPath}.`);
46 }
47 const next = source.replace(displayNamePattern, `$1${escapeXml(displayName)}$2`);
48 if (next !== source) {
49 fs.writeFileSync(plistPath, next);
50 }
51 return cfg;
52 },
53 ]);
55​
56function withExtensionDisplayNameBuildSetting(config, displayName) {
57 return withXcodeProject(config, (cfg) => {
58 const objects = cfg.modResults.hash.project.objects;
59 const targetEntry = findByName(objects.PBXNativeTarget, TARGET_NAME);
60 if (!targetEntry) {
61 throw new Error(
62 `${TARGET_NAME} Xcode target was not generated. Register withShareExtensionDisplayName before expo-sharing.`,
63 );
64 }
65 const target = targetEntry[1];
66 const configurationList = stripComments(objects.XCConfigurationList)[
67 target.buildConfigurationList
68 ];
69 if (!configurationList) {
70 throw new Error(`Could not find build configurations for ${TARGET_NAME}.`);
71 }
72 const buildConfigurations = stripComments(objects.XCBuildConfiguration);
73 for (const reference of configurationList.buildConfigurations ?? []) {
74 const buildConfiguration = buildConfigurations[reference.value];
75 if (buildConfiguration?.buildSettings) {
76 buildConfiguration.buildSettings.INFOPLIST_KEY_CFBundleDisplayName = `"${displayName}"`;
77 }
78 }
79 return cfg;
80 });
82​
83module.exports = function withShareExtensionDisplayName(config) {
84 const displayName = config.name;
85 return withExtensionDisplayNameBuildSetting(
86 withDisplayNamePlist(config, displayName),
87 displayName,
88 );
89};