Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 888684e

Browse files
hilkeheremansRichard Hua
authored andcommitted
Fix linking errors with multiple AppDelegate.m (#564)
In issue #477 there was an issue when the project has, for some reason, multiple AppDelegate.m files (eg project sample files) within the ios subdir of an RN project. This fix will give priority to the AppDelegate.m found inside any path that has the application name (as defined in package.json) in it. If it cannot find that, it will revert to previous behavior (the first element in the path array).
1 parent c44a252 commit 888684e

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

scripts/postlink/ios/postlink.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ var glob = require("glob");
33
var inquirer = require('inquirer');
44
var path = require("path");
55
var plist = require("plist");
6+
var package = require('../../../../../package.json');
67

78
var ignoreNodeModules = { ignore: "node_modules/**" };
8-
var appDelegatePath = glob.sync("**/AppDelegate.m", ignoreNodeModules)[0];
9+
var appDelegatePaths = glob.sync("**/AppDelegate.m", ignoreNodeModules);
10+
11+
// Fix for https://github.com/Microsoft/react-native-code-push/issues/477
12+
// Typical location of AppDelegate.m for newer RN versions: $PROJECT_ROOT/ios/<project_name>/AppDelegate.m
13+
// Let's try to find that path by filtering the whole array for any path containing <project_name>
14+
// If we can't find it there, play dumb and pray it is the first path we find.
15+
var appDelegatePath = findFileByAppName(appDelegatePaths, package ? package.name : null) || appDelegatePaths[0];
16+
917
// Glob only allows foward slashes in patterns: https://www.npmjs.com/package/glob#windows
1018
var plistPath = glob.sync(path.join(path.dirname(appDelegatePath), "*Info.plist").replace(/\\/g, "/"), ignoreNodeModules)[0];
1119

@@ -59,4 +67,19 @@ if (parsedInfoPlist.CodePushDeploymentKey) {
5967
function writePatches() {
6068
fs.writeFileSync(appDelegatePath, appDelegateContents);
6169
fs.writeFileSync(plistPath, plistContents);
62-
}
70+
}
71+
72+
// Helper that filters an array with AppDelegate.m paths for a path with the app name inside it
73+
// Should cover nearly all cases
74+
function findFileByAppName(array, appName) {
75+
if (array.length === 0 || !appName) return null;
76+
77+
for (var i = 0; i < array.length; i++) {
78+
var path = array[i];
79+
if (path && path.indexOf(appName) !== -1) {
80+
return path;
81+
}
82+
}
83+
84+
return null;
85+
}

0 commit comments

Comments
 (0)