|
| 1 | +// Expo Config Plugin that re-applies Rock's autolinking patches on |
| 2 | +// every `expo prebuild`. Designed to live in `app.config.ts plugins: |
| 3 | +// ['@rock-js/plugin-expo-config-plugins']` so the patches survive |
| 4 | +// Expo CNG (Continuous Native Generation) regenerating `ios/` and |
| 5 | +// `android/` from scratch. |
| 6 | +// |
| 7 | +// Mirrors the one-shot transforms in |
| 8 | +// `packages/create-app/src/lib/utils/initInExistingProject.ts`: |
| 9 | +// - iOS Podfile — point `use_native_modules!` at Rock |
| 10 | +// - Android build.gradle — point `cliFile` at Rock's bin.js |
| 11 | +// - Android settings.gradle — point `autolinkLibrariesFromCommand` |
| 12 | +// at Rock |
| 13 | +// - Xcode project.pbxproj — rewrite the React Native build phase |
| 14 | +// shellScript to source Rock's CLI |
| 15 | +// |
| 16 | +// Each step is idempotent so repeated prebuilds don't break. |
| 17 | + |
| 18 | +import { type ConfigPlugin, withDangerousMod } from '@expo/config-plugins'; |
| 19 | +import * as fs from 'node:fs'; |
| 20 | +import * as path from 'node:path'; |
| 21 | + |
| 22 | +// ----- iOS / Podfile --------------------------------------------------------- |
| 23 | + |
| 24 | +export function patchPodfile(content: string): string { |
| 25 | + if (content.includes(`(['npx', 'rock', 'config', '-p', 'ios'])`)) { |
| 26 | + // Already patched — leave alone (idempotent). |
| 27 | + return content; |
| 28 | + } |
| 29 | + return content.replace( |
| 30 | + /(config\s*=\s*use_native_modules!)(\s*\([^)]*\))?/g, |
| 31 | + "$1(['npx', 'rock', 'config', '-p', 'ios'])", |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +const withRockIosPodfile: ConfigPlugin = (config) => |
| 36 | + withDangerousMod(config, [ |
| 37 | + 'ios', |
| 38 | + async (cfg) => { |
| 39 | + const podfilePath = path.join(cfg.modRequest.platformProjectRoot, 'Podfile'); |
| 40 | + if (!fs.existsSync(podfilePath)) return cfg; |
| 41 | + const original = await fs.promises.readFile(podfilePath, 'utf8'); |
| 42 | + const patched = patchPodfile(original); |
| 43 | + if (patched !== original) { |
| 44 | + await fs.promises.writeFile(podfilePath, patched); |
| 45 | + } |
| 46 | + return cfg; |
| 47 | + }, |
| 48 | + ]); |
| 49 | + |
| 50 | +// ----- iOS / Xcode project.pbxproj ------------------------------------------ |
| 51 | + |
| 52 | +const XCODE_REACT_PHASE_TARGET = |
| 53 | + 'shellScript = "set -e\\nif [[ -f \\"$PODS_ROOT/../.xcode.env\\" ]]; then\\nsource \\"$PODS_ROOT/../.xcode.env\\"\\nfi\\nif [[ -f \\"$PODS_ROOT/../.xcode.env.local\\" ]]; then\\nsource \\"$PODS_ROOT/../.xcode.env.local\\"\\nfi\\nexport CONFIG_CMD=\\"dummy-workaround-value\\"\\nexport CLI_PATH=\\"$(\\"$NODE_BINARY\\" --print \\"require(\'path\').dirname(require.resolve(\'rock/package.json\')) + \'/dist/src/bin.js\'\\")\\"\\nWITH_ENVIRONMENT=\\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\\"\\n";'; |
| 54 | + |
| 55 | +const XCODE_REACT_PHASE_SOURCES = [ |
| 56 | + // Default Expo / Community-CLI format |
| 57 | + 'shellScript = "set -e\\n\\nWITH_ENVIRONMENT=\\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\\"\\nREACT_NATIVE_XCODE=\\"$REACT_NATIVE_PATH/scripts/react-native-xcode.sh\\"\\n\\n/bin/sh -c \\"$WITH_ENVIRONMENT $REACT_NATIVE_XCODE\\"\\n";', |
| 58 | + // RN 0.83 format |
| 59 | + 'shellScript = "set -e\\n\\nWITH_ENVIRONMENT=\\"$REACT_NATIVE_PATH/scripts/xcode/with-environment.sh\\"\\nREACT_NATIVE_XCODE=\\"$REACT_NATIVE_PATH/scripts/react-native-xcode.sh\\"\\n\\n/bin/sh -c \\"\\\\\\"$WITH_ENVIRONMENT\\\\\\" \\\\\\"$REACT_NATIVE_XCODE\\\\\\"\\"\\n";', |
| 60 | +]; |
| 61 | + |
| 62 | +export function patchXcodeProject(content: string): string { |
| 63 | + if (content.includes(XCODE_REACT_PHASE_TARGET)) { |
| 64 | + return content; |
| 65 | + } |
| 66 | + for (const source of XCODE_REACT_PHASE_SOURCES) { |
| 67 | + if (content.includes(source)) { |
| 68 | + return content.replace(source, XCODE_REACT_PHASE_TARGET); |
| 69 | + } |
| 70 | + } |
| 71 | + return content; |
| 72 | +} |
| 73 | + |
| 74 | +const withRockIosXcode: ConfigPlugin = (config) => |
| 75 | + withDangerousMod(config, [ |
| 76 | + 'ios', |
| 77 | + async (cfg) => { |
| 78 | + const iosDir = cfg.modRequest.platformProjectRoot; |
| 79 | + const xcodeProjectFolder = (await fs.promises.readdir(iosDir)).find((f) => |
| 80 | + f.endsWith('.xcodeproj'), |
| 81 | + ); |
| 82 | + if (!xcodeProjectFolder) return cfg; |
| 83 | + const projectPath = path.join(iosDir, xcodeProjectFolder, 'project.pbxproj'); |
| 84 | + if (!fs.existsSync(projectPath)) return cfg; |
| 85 | + const original = await fs.promises.readFile(projectPath, 'utf8'); |
| 86 | + const patched = patchXcodeProject(original); |
| 87 | + if (patched !== original) { |
| 88 | + await fs.promises.writeFile(projectPath, patched); |
| 89 | + } |
| 90 | + return cfg; |
| 91 | + }, |
| 92 | + ]); |
| 93 | + |
| 94 | +// ----- Android / app/build.gradle ------------------------------------------- |
| 95 | + |
| 96 | +const ANDROID_CLI_FILE_TARGET = 'cliFile = file("../../node_modules/rock/dist/src/bin.js")'; |
| 97 | + |
| 98 | +export function patchAndroidBuildGradle(content: string): string { |
| 99 | + if (content.includes(ANDROID_CLI_FILE_TARGET)) { |
| 100 | + return content; |
| 101 | + } |
| 102 | + return content.replace( |
| 103 | + /(?:\/\/\s+)?cliFile\s*=\s*file\([^)]*\)/g, |
| 104 | + ANDROID_CLI_FILE_TARGET, |
| 105 | + ); |
| 106 | +} |
| 107 | + |
| 108 | +// ----- Android / settings.gradle -------------------------------------------- |
| 109 | + |
| 110 | +const ANDROID_AUTOLINK_TARGET = |
| 111 | + "autolinkLibrariesFromCommand(['npx', 'rock', 'config', '-p', 'android'])"; |
| 112 | + |
| 113 | +export function patchAndroidSettingsGradle(content: string): string { |
| 114 | + if (content.includes(ANDROID_AUTOLINK_TARGET)) { |
| 115 | + return content; |
| 116 | + } |
| 117 | + // Try the full block first (`extensions.configure(...) { ... }`)… |
| 118 | + const blockPattern = |
| 119 | + /extensions\.configure\(com\.facebook\.react\.ReactSettingsExtension\)\{[^}]*autolinkLibrariesFromCommand\([^)]*\)[^}]*\}/gs; |
| 120 | + const blockReplacement = `extensions.configure(com.facebook.react.ReactSettingsExtension){ ex -> ex.${ANDROID_AUTOLINK_TARGET} }`; |
| 121 | + if (blockPattern.test(content)) { |
| 122 | + return content.replace(blockPattern, blockReplacement); |
| 123 | + } |
| 124 | + // …otherwise fall back to replacing just the inner call. |
| 125 | + return content.replace(/autolinkLibrariesFromCommand\([^)]*\)/g, ANDROID_AUTOLINK_TARGET); |
| 126 | +} |
| 127 | + |
| 128 | +const withRockAndroid: ConfigPlugin = (config) => |
| 129 | + withDangerousMod(config, [ |
| 130 | + 'android', |
| 131 | + async (cfg) => { |
| 132 | + const androidDir = cfg.modRequest.platformProjectRoot; |
| 133 | + const appBuildGradlePath = path.join(androidDir, 'app', 'build.gradle'); |
| 134 | + const settingsGradlePath = path.join(androidDir, 'settings.gradle'); |
| 135 | + |
| 136 | + if (fs.existsSync(appBuildGradlePath)) { |
| 137 | + const original = await fs.promises.readFile(appBuildGradlePath, 'utf8'); |
| 138 | + const patched = patchAndroidBuildGradle(original); |
| 139 | + if (patched !== original) { |
| 140 | + await fs.promises.writeFile(appBuildGradlePath, patched); |
| 141 | + } |
| 142 | + } |
| 143 | + if (fs.existsSync(settingsGradlePath)) { |
| 144 | + const original = await fs.promises.readFile(settingsGradlePath, 'utf8'); |
| 145 | + const patched = patchAndroidSettingsGradle(original); |
| 146 | + if (patched !== original) { |
| 147 | + await fs.promises.writeFile(settingsGradlePath, patched); |
| 148 | + } |
| 149 | + } |
| 150 | + return cfg; |
| 151 | + }, |
| 152 | + ]); |
| 153 | + |
| 154 | +// ----- Top-level plugin ----------------------------------------------------- |
| 155 | + |
| 156 | +/** |
| 157 | + * Expo Config Plugin that re-applies Rock's autolinking patches to the |
| 158 | + * native dirs generated by `expo prebuild`. Add it once to |
| 159 | + * `app.config.ts` `plugins`: |
| 160 | + * |
| 161 | + * plugins: [ |
| 162 | + * '@rock-js/plugin-expo-config-plugins/withRockAutolinking', |
| 163 | + * // … |
| 164 | + * ], |
| 165 | + * |
| 166 | + * Idempotent — repeated `expo prebuild --clean` runs converge on the |
| 167 | + * same patched files. |
| 168 | + */ |
| 169 | +export const withRockAutolinking: ConfigPlugin = (config) => { |
| 170 | + config = withRockIosPodfile(config); |
| 171 | + config = withRockIosXcode(config); |
| 172 | + config = withRockAndroid(config); |
| 173 | + return config; |
| 174 | +}; |
| 175 | + |
| 176 | +export default withRockAutolinking; |
0 commit comments