Files
react-native/packages/react-native/scripts/swiftpm/prepare-app-utils.js
T
Riccardo Cipolleschi f0725f7802 Add script to find the directory that contains the Xcodeproj (#53669)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53669

## Context

When configuring an app to build with SwiftPM from source, there is a sequence of operations we need to run in order to prepare the project correctly.

## Changed

Add a function that given the root of the app and the name of the xcodeproject file, can return the path to the Xcode project file

## Changelog:
[Internal] -

Reviewed By: cortinico

Differential Revision: D81778456

fbshipit-source-id: f7050bcb049d75a5b1cabf340a5b98f4736e60b3
2025-10-10 11:19:38 -07:00

47 lines
1.4 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
const {execSync} = require('child_process');
const path = require('path');
/**
* Find the directory containing the Xcode project within the app path
* @param {string} appPath - The root app path to search in
* @param {string} xcodeProjectName - The name of the Xcode project file (e.g., 'HelloWorld.xcodeproj')
* @returns {string} - The path to the directory containing the Xcode project
*/
function findXcodeProjectDirectory(
appPath /*: string */,
xcodeProjectName /*: string */,
) /*: string */ {
try {
// Use find command to search for the Xcode project
const findCommand = `find "${appPath}" -name "${xcodeProjectName}" -type d -print`;
const result = execSync(findCommand, {encoding: 'utf8'}).trim();
if (!result) {
throw new Error(
`Xcode project '${xcodeProjectName}' not found in '${appPath}' or its subdirectories`,
);
}
// Return the directory containing the Xcode project (parent of the .xcodeproj file)
return path.dirname(result);
} catch (error) {
throw new Error(
`Failed to find Xcode project '${xcodeProjectName}': ${error.message}`,
);
}
}
module.exports = {
findXcodeProjectDirectory,
};