Skip to content

Commit 0d5bd2a

Browse files
authored
Use mergeContents instead of appending code to the end of file in withPodfile (#57)
* Use mergeContents instead of appending in withPodfile * Add indents to merged podfile contents
1 parent 73d9c19 commit 0d5bd2a

File tree

1 file changed

+51
-47
lines changed

1 file changed

+51
-47
lines changed

plugin/src/withPodfile.ts

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mergeContents } from "@expo/config-plugins/build/utils/generateCode";
12
import { type ConfigPlugin, withDangerousMod } from "expo/config-plugins";
23
import fs from "node:fs";
34
import path from "node:path";
@@ -19,65 +20,68 @@ export const withPodfile: ConfigPlugin<{
1920

2021
const useExpoModules =
2122
excludedPackages && excludedPackages.length > 0
22-
? `exclude = ["${excludedPackages.join(`", "`)}"]\n use_expo_modules!(exclude: exclude)`
23+
? `exclude = ["${excludedPackages.join(`", "`)}"]\n use_expo_modules!(exclude: exclude)`
2324
: "use_expo_modules!";
2425

2526
const appClipTarget = `
26-
target '${targetName}' do
27-
${useExpoModules}
27+
target '${targetName}' do
28+
${useExpoModules}
2829
29-
if ENV['EXPO_USE_COMMUNITY_AUTOLINKING'] == '1'
30-
config_command = ['node', '-e', "process.argv=['', '', 'config'];require('@react-native-community/cli').run()"];
31-
else
32-
config_command = [
30+
if ENV['EXPO_USE_COMMUNITY_AUTOLINKING'] == '1'
31+
config_command = ['node', '-e', "process.argv=['', '', 'config'];require('@react-native-community/cli').run()"];
32+
else
33+
config_command = [
34+
'node',
35+
'--no-warnings',
36+
'--eval',
37+
'require(require.resolve(\\'expo-modules-autolinking\\', { paths: [require.resolve(\\'expo/package.json\\')] }))(process.argv.slice(1))',
38+
'react-native-config',
39+
'--json',
40+
'--platform',
41+
'ios'
42+
]
43+
end
44+
45+
# Running the command in the same manner as \`use_react_native\` then running that result through our cliPlugin
46+
json, message, status = Pod::Executable.capture_command(config_command[0], config_command[1..], capture: :both)
47+
if not status.success?
48+
Pod::UI.warn "The command: '#{config_command.join(" ").bold.yellow}' returned a status code of #{status.exitstatus.to_s.bold.red}, #{message}", [
49+
"App Clip autolinking failed. Please ensure autolinking works correctly for the main app target and try again.",
50+
]
51+
exit(status.exitstatus)
52+
end
53+
54+
# \`react-native-app-clip\` resolves to react-native-app-clip/build/index.js
55+
clip_command = [
3356
'node',
3457
'--no-warnings',
3558
'--eval',
36-
'require(require.resolve(\\'expo-modules-autolinking\\', { paths: [require.resolve(\\'expo/package.json\\')] }))(process.argv.slice(1))',
37-
'react-native-config',
38-
'--json',
39-
'--platform',
40-
'ios'
41-
]
42-
end
43-
44-
# Running the command in the same manner as \`use_react_native\` then running that result through our cliPlugin
45-
json, message, status = Pod::Executable.capture_command(config_command[0], config_command[1..], capture: :both)
46-
if not status.success?
47-
Pod::UI.warn "The command: '#{config_command.join(" ").bold.yellow}' returned a status code of #{status.exitstatus.to_s.bold.red}, #{message}", [
48-
"App Clip autolinking failed. Please ensure autolinking works correctly for the main app target and try again.",
59+
'require(require.resolve(\\'react-native-app-clip\\')+\\'/../../plugin/build/cliPlugin.js\\').run(' + json + ', [${(excludedPackages ?? []).map((packageName) => `"${packageName}"`).join(", ")}])'
4960
]
50-
exit(status.exitstatus)
51-
end
5261
53-
# \`react-native-app-clip\` resolves to react-native-app-clip/build/index.js
54-
clip_command = [
55-
'node',
56-
'--no-warnings',
57-
'--eval',
58-
'require(require.resolve(\\'react-native-app-clip\\')+\\'/../../plugin/build/cliPlugin.js\\').run(' + json + ', [${(excludedPackages ?? []).map((packageName) => `"${packageName}"`).join(", ")}])'
59-
]
62+
config = use_native_modules!(clip_command)
6063
61-
config = use_native_modules!(clip_command)
64+
use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']
65+
use_frameworks! :linkage => ENV['USE_FRAMEWORKS'].to_sym if ENV['USE_FRAMEWORKS']
6266
63-
use_frameworks! :linkage => podfile_properties['ios.useFrameworks'].to_sym if podfile_properties['ios.useFrameworks']
64-
use_frameworks! :linkage => ENV['USE_FRAMEWORKS'].to_sym if ENV['USE_FRAMEWORKS']
65-
66-
use_react_native!(
67-
:path => config[:reactNativePath],
68-
:hermes_enabled => podfile_properties['expo.jsEngine'] == nil || podfile_properties['expo.jsEngine'] == 'hermes',
69-
# An absolute path to your application root.
70-
:app_path => "#{Pod::Config.instance.installation_root}/..",
71-
:privacy_file_aggregation_enabled => podfile_properties['apple.privacyManifestAggregationEnabled'] != 'false',
72-
)
73-
end
74-
`.trim();
67+
use_react_native!(
68+
:path => config[:reactNativePath],
69+
:hermes_enabled => podfile_properties['expo.jsEngine'] == nil || podfile_properties['expo.jsEngine'] == 'hermes',
70+
# An absolute path to your application root.
71+
:app_path => "#{Pod::Config.instance.installation_root}/..",
72+
:privacy_file_aggregation_enabled => podfile_properties['apple.privacyManifestAggregationEnabled'] != 'false',
73+
)
74+
end
75+
`;
7576

76-
// It's generally safer to use `mergeContents` to insert new content into the Podfile, but this new target should always be inserted at the end of the file.
77-
podfileContent = podfileContent
78-
.concat(`\n\n# >>> Inserted by react-native-app-clip\n`)
79-
.concat(appClipTarget)
80-
.concat(`\n\n# <<< Inserted by react-native-app-clip\n`);
77+
podfileContent = mergeContents({
78+
tag: "Generated by react-native-app-clip",
79+
src: podfileContent,
80+
newSrc: appClipTarget,
81+
anchor: "use_expo_modules!",
82+
offset: 0,
83+
comment: "#",
84+
}).contents;
8185

8286
fs.writeFileSync(podFilePath, podfileContent);
8387

0 commit comments

Comments
 (0)