Skip to content

fix(ios): fix run-ios failing with @react-native-community/cli 5 #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion example/react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,26 @@ module.exports = {
),
},
ios: {
project: "ios/ReactTestApp-Dummy.xcodeproj",
project: (() => {
const {
packageSatisfiesVersionRange,
} = require("react-native-test-app/scripts/configure");
if (
packageSatisfiesVersionRange(
"@react-native-community/cli-platform-ios",
"<5.0.2"
)
) {
// Prior to @react-native-community/cli-platform-ios v5.0.0,
// `project` was only used to infer `sourceDir` and `podfile`.
return "ios/ReactTestApp-Dummy.xcodeproj";
}

// `sourceDir` and `podfile` detection was fixed in
// @react-native-community/cli-platform-ios v5.0.2 (see
// https://github.com/react-native-community/cli/pull/1444).
return "node_modules/.generated/ios/ReactTestApp.xcodeproj";
})(),
},
windows: fs.existsSync(windowsProjectFile) && {
sourceDir: "windows",
Expand Down
60 changes: 40 additions & 20 deletions scripts/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ function readJSONFile(path) {
return JSON.parse(fs.readFileSync(path, { encoding: "utf-8" }));
}

/**
* Returns whether installed package satisfies specified version range.
* @param {string} pkg
* @param {string} versionRange
* @returns {boolean}
*/
function packageSatisfiesVersionRange(pkg, versionRange) {
const { version } = require(`${pkg}/package.json`);
return semver.satisfies(version, versionRange);
}

/**
* Converts an object or value to a pretty JSON string.
* @param {Record<string, unknown>} obj
Expand Down Expand Up @@ -239,10 +250,18 @@ function reactNativeConfig({ name, testAppPath, platforms, flatten }) {
case "macos":
case "ios":
return join(
"const {",
" packageSatisfiesVersionRange,",
'} = require("react-native-test-app/scripts/configure");',
"module.exports = {",
" project: {",
" ios: {",
` project: "ReactTestApp-Dummy.xcodeproj"`,
" project: packageSatisfiesVersionRange(",
' "@react-native-community/cli-platform-ios",',
' "<5.0.2"',
" )",
' ? "ReactTestApp-Dummy.xcodeproj"',
' : "node_modules/.generated/ios/ReactTestApp.xcodeproj",',
" }",
" }",
"};",
Expand Down Expand Up @@ -889,22 +908,23 @@ if (require.main === module) {
).argv;
}

exports["configure"] = configure;
exports["error"] = error;
exports["gatherConfig"] = gatherConfig;
exports["getAppName"] = getAppName;
exports["getConfig"] = getConfig;
exports["getPlatformPackage"] = getPlatformPackage;
exports["getTargetReactNativeVersion"] = getTargetReactNativeVersion;
exports["isDestructive"] = isDestructive;
exports["isInstalled"] = isInstalled;
exports["join"] = join;
exports["mergeConfig"] = mergeConfig;
exports["projectRelativePath"] = projectRelativePath;
exports["reactNativeConfig"] = reactNativeConfig;
exports["readJSONFile"] = readJSONFile;
exports["removeAllFiles"] = removeAllFiles;
exports["sortByKeys"] = sortByKeys;
exports["updatePackageManifest"] = updatePackageManifest;
exports["warn"] = warn;
exports["writeAllFiles"] = writeAllFiles;
exports.configure = configure;
exports.error = error;
exports.gatherConfig = gatherConfig;
exports.getAppName = getAppName;
exports.getConfig = getConfig;
exports.getPlatformPackage = getPlatformPackage;
exports.getTargetReactNativeVersion = getTargetReactNativeVersion;
exports.isDestructive = isDestructive;
exports.isInstalled = isInstalled;
exports.join = join;
exports.mergeConfig = mergeConfig;
exports.packageSatisfiesVersionRange = packageSatisfiesVersionRange;
exports.projectRelativePath = projectRelativePath;
exports.reactNativeConfig = reactNativeConfig;
exports.readJSONFile = readJSONFile;
exports.removeAllFiles = removeAllFiles;
exports.sortByKeys = sortByKeys;
exports.updatePackageManifest = updatePackageManifest;
exports.warn = warn;
exports.writeAllFiles = writeAllFiles;
96 changes: 90 additions & 6 deletions test/configure/__snapshots__/gatherConfig.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ use_test_app!
"metro.config.js": Object {
"source": "example/metro.config.js",
},
"react-native.config.js": "module.exports = {
"react-native.config.js": "const {
packageSatisfiesVersionRange,
} = require(\\"react-native-test-app/scripts/configure\\");
module.exports = {
project: {
ios: {
project: \\"ReactTestApp-Dummy.xcodeproj\\"
project: packageSatisfiesVersionRange(
\\"@react-native-community/cli-platform-ios\\",
\\"<5.0.2\\"
)
? \\"ReactTestApp-Dummy.xcodeproj\\"
: \\"node_modules/.generated/ios/ReactTestApp.xcodeproj\\",
}
}
};
Expand Down Expand Up @@ -142,7 +150,26 @@ module.exports = {
),
},
ios: {
project: \\"ios/ReactTestApp-Dummy.xcodeproj\\",
project: (() => {
const {
packageSatisfiesVersionRange,
} = require(\\"react-native-test-app/scripts/configure\\");
if (
packageSatisfiesVersionRange(
\\"@react-native-community/cli-platform-ios\\",
\\"<5.0.2\\"
)
) {
// Prior to @react-native-community/cli-platform-ios v5.0.0,
// \`project\` was only used to infer \`sourceDir\` and \`podfile\`.
return \\"ios/ReactTestApp-Dummy.xcodeproj\\";
}

// \`sourceDir\` and \`podfile\` detection was fixed in
// @react-native-community/cli-platform-ios v5.0.2 (see
// https://github.com/react-native-community/cli/pull/1444).
return \\"node_modules/.generated/ios/ReactTestApp.xcodeproj\\";
})(),
},
windows: fs.existsSync(windowsProjectFile) && {
sourceDir: \\"windows\\",
Expand Down Expand Up @@ -226,7 +253,26 @@ module.exports = {
),
},
ios: {
project: \\"ios/ReactTestApp-Dummy.xcodeproj\\",
project: (() => {
const {
packageSatisfiesVersionRange,
} = require(\\"react-native-test-app/scripts/configure\\");
if (
packageSatisfiesVersionRange(
\\"@react-native-community/cli-platform-ios\\",
\\"<5.0.2\\"
)
) {
// Prior to @react-native-community/cli-platform-ios v5.0.0,
// \`project\` was only used to infer \`sourceDir\` and \`podfile\`.
return \\"ios/ReactTestApp-Dummy.xcodeproj\\";
}

// \`sourceDir\` and \`podfile\` detection was fixed in
// @react-native-community/cli-platform-ios v5.0.2 (see
// https://github.com/react-native-community/cli/pull/1444).
return \\"node_modules/.generated/ios/ReactTestApp.xcodeproj\\";
})(),
},
windows: fs.existsSync(windowsProjectFile) && {
sourceDir: \\"windows\\",
Expand Down Expand Up @@ -360,7 +406,26 @@ module.exports = {
),
},
ios: {
project: \\"ios/ReactTestApp-Dummy.xcodeproj\\",
project: (() => {
const {
packageSatisfiesVersionRange,
} = require(\\"react-native-test-app/scripts/configure\\");
if (
packageSatisfiesVersionRange(
\\"@react-native-community/cli-platform-ios\\",
\\"<5.0.2\\"
)
) {
// Prior to @react-native-community/cli-platform-ios v5.0.0,
// \`project\` was only used to infer \`sourceDir\` and \`podfile\`.
return \\"ios/ReactTestApp-Dummy.xcodeproj\\";
}

// \`sourceDir\` and \`podfile\` detection was fixed in
// @react-native-community/cli-platform-ios v5.0.2 (see
// https://github.com/react-native-community/cli/pull/1444).
return \\"node_modules/.generated/ios/ReactTestApp.xcodeproj\\";
})(),
},
windows: fs.existsSync(windowsProjectFile) && {
sourceDir: \\"windows\\",
Expand Down Expand Up @@ -501,7 +566,26 @@ module.exports = {
),
},
ios: {
project: \\"ios/ReactTestApp-Dummy.xcodeproj\\",
project: (() => {
const {
packageSatisfiesVersionRange,
} = require(\\"react-native-test-app/scripts/configure\\");
if (
packageSatisfiesVersionRange(
\\"@react-native-community/cli-platform-ios\\",
\\"<5.0.2\\"
)
) {
// Prior to @react-native-community/cli-platform-ios v5.0.0,
// \`project\` was only used to infer \`sourceDir\` and \`podfile\`.
return \\"ios/ReactTestApp-Dummy.xcodeproj\\";
}

// \`sourceDir\` and \`podfile\` detection was fixed in
// @react-native-community/cli-platform-ios v5.0.2 (see
// https://github.com/react-native-community/cli/pull/1444).
return \\"node_modules/.generated/ios/ReactTestApp.xcodeproj\\";
})(),
},
windows: fs.existsSync(windowsProjectFile) && {
sourceDir: \\"windows\\",
Expand Down