From 08d3a36f535a8bb6d8ee92089ea6a3cb98600769 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 23 Oct 2024 01:35:42 +0200 Subject: [PATCH] fix(react-native): implement `projectConfig` and `dependencyConfig` (#2231) --- .../local-cli/runMacOS/findXcodeProject.js | 37 ----------- .../local-cli/runMacOS/runMacOS.js | 65 +++++++++++-------- packages/react-native/package.json | 3 +- packages/react-native/react-native.config.js | 8 ++- yarn.lock | 1 + 5 files changed, 47 insertions(+), 67 deletions(-) delete mode 100644 packages/react-native/local-cli/runMacOS/findXcodeProject.js diff --git a/packages/react-native/local-cli/runMacOS/findXcodeProject.js b/packages/react-native/local-cli/runMacOS/findXcodeProject.js deleted file mode 100644 index 8f0e3162e3f776..00000000000000 --- a/packages/react-native/local-cli/runMacOS/findXcodeProject.js +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright (c) Microsoft Corporation. All rights reserved. - * Licensed under the MIT License. - * @format - * @ts-check - */ -'use strict'; - -const path = require('path'); - -/** - * @param {string[]} files - */ -function findXcodeProject(files) { - const sortedFiles = files.sort(); - for (let i = sortedFiles.length - 1; i >= 0; i--) { - const fileName = files[i]; - const ext = path.extname(fileName); - - if (ext === '.xcworkspace') { - return { - name: fileName, - isWorkspace: true, - }; - } - if (ext === '.xcodeproj') { - return { - name: fileName, - isWorkspace: false, - }; - } - } - - return null; -} - -module.exports = findXcodeProject; diff --git a/packages/react-native/local-cli/runMacOS/runMacOS.js b/packages/react-native/local-cli/runMacOS/runMacOS.js index 0089810dfe25f1..2b3088108650b6 100644 --- a/packages/react-native/local-cli/runMacOS/runMacOS.js +++ b/packages/react-native/local-cli/runMacOS/runMacOS.js @@ -1,8 +1,8 @@ +// @ts-check /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. * @format - * @ts-check */ 'use strict'; @@ -20,14 +20,22 @@ * * @typedef {{ * name: string; + * path: string; * isWorkspace: boolean; * }} XcodeProject + * + * @typedef {{ + * project?: { + * macos?: { + * sourceDir: string; + * xcodeProject: XcodeProject; + * } + * } + * }} ProjectConfig */ -const findXcodeProject = require('./findXcodeProject'); const chalk = require('chalk'); const child_process = require('child_process'); -const fs = require('fs'); const path = require('path'); const {logger, CLIError, getDefaultUserTerminal} = (() => { const cli = require.resolve('@react-native-community/cli/package.json'); @@ -37,16 +45,11 @@ const {logger, CLIError, getDefaultUserTerminal} = (() => { })(); /** + * @param {ProjectConfig} ctx * @param {Options} args - * @returns {{ xcodeProject: XcodeProject, scheme: string }} + * @returns {{ sourceDir: string, xcodeProject: XcodeProject, scheme: string }} */ -function parseArgs(args) { - if (!fs.existsSync(args.projectPath)) { - throw new CLIError( - 'macOS project folder not found. Are you sure this is a React Native project?', - ); - } - +function parseArgs(ctx, args) { if (args.configuration) { logger.warn( 'Argument --configuration has been deprecated and will be removed in a future release, please use --mode instead.', @@ -57,15 +60,20 @@ function parseArgs(args) { } } - process.chdir(args.projectPath); + const {sourceDir, xcodeProject} = ctx.project?.macos ?? {}; + if (!sourceDir) { + throw new CLIError( + 'macOS project folder not found. Are you sure this is a React Native project?', + ); + } - const xcodeProject = findXcodeProject(fs.readdirSync('.')); if (!xcodeProject) { throw new CLIError( - `Could not find Xcode project files in "${args.projectPath}" folder`, + 'Xcode project for macOS not found. Did you forget to run `pod install`?', ); } + // TODO: Find schemes using https://github.com/microsoft/rnx-kit/blob/2b4c569cda9e3755ba7afce42d846601bcc7c4e3/packages/tools-apple/src/scheme.ts#L5 const inferredSchemeName = path.basename(xcodeProject.name, path.extname(xcodeProject.name)) + '-macOS'; @@ -77,36 +85,37 @@ function parseArgs(args) { } "${chalk.bold(xcodeProject.name)}"`, ); - return {xcodeProject, scheme}; + return {sourceDir, xcodeProject, scheme}; } /** * @param {string[]} _ - * @param {Record} _ctx + * @param {ProjectConfig} ctx * @param {Options} args */ -function buildMacOS(_, _ctx, args) { - const {xcodeProject, scheme} = parseArgs(args); - return buildProject(xcodeProject, scheme, {...args, packager: false}); +function buildMacOS(_, ctx, args) { + const {sourceDir, xcodeProject, scheme} = parseArgs(ctx, args); + return buildProject(sourceDir, xcodeProject, scheme, {...args, packager: false}); } /** * @param {string[]} _ - * @param {Record} _ctx + * @param {ProjectConfig} ctx * @param {Options} args */ -function runMacOS(_, _ctx, args) { - const {xcodeProject, scheme} = parseArgs(args); - return run(xcodeProject, scheme, args); +function runMacOS(_, ctx, args) { + const {sourceDir, xcodeProject, scheme} = parseArgs(ctx, args); + return run(sourceDir, xcodeProject, scheme, args); } /** + * @param {string} sourceDir * @param {XcodeProject} xcodeProject * @param {string} scheme * @param {Options} args */ -async function run(xcodeProject, scheme, args) { - await buildProject(xcodeProject, scheme, args); +async function run(sourceDir, xcodeProject, scheme, args) { + await buildProject(sourceDir, xcodeProject, scheme, args); const buildSettings = getBuildSettings(xcodeProject, args.mode, scheme); const appPath = path.join( @@ -143,15 +152,17 @@ async function run(xcodeProject, scheme, args) { } /** + * @param {string} sourceDir * @param {XcodeProject} xcodeProject * @param {string} scheme * @param {Options} args + * @returns {Promise} */ -function buildProject(xcodeProject, scheme, args) { +function buildProject(sourceDir, xcodeProject, scheme, args) { return new Promise((resolve, reject) => { const xcodebuildArgs = [ xcodeProject.isWorkspace ? '-workspace' : '-project', - xcodeProject.name, + path.join(sourceDir, xcodeProject.path, xcodeProject.name), '-configuration', args.mode, '-scheme', diff --git a/packages/react-native/package.json b/packages/react-native/package.json index a05bdbc52fd39e..962a0d2ca310a7 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -110,6 +110,7 @@ "@jest/create-cache-key-function": "^29.6.3", "@react-native-community/cli": "13.6.9", "@react-native-community/cli-platform-android": "13.6.9", + "@react-native-community/cli-platform-apple": "13.6.9", "@react-native-community/cli-platform-ios": "13.6.9", "@react-native-mac/virtualized-lists": "0.74.87", "@react-native/assets-registry": "0.74.87", @@ -186,4 +187,4 @@ } ] } -} \ No newline at end of file +} diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 644d850d64bf95..7944a0498b7249 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -11,6 +11,10 @@ const android = require('@react-native-community/cli-platform-android'); const ios = require('@react-native-community/cli-platform-ios'); +const { + getDependencyConfig, + getProjectConfig, +} = require('@react-native-community/cli-platform-apple'); // [macOS] const { bundleCommand, ramBundleCommand, @@ -95,8 +99,8 @@ module.exports = { ) => {}, }; }, - projectConfig: () => null, - dependencyConfig: () => null, + projectConfig: getProjectConfig({platformName: 'macos'}), + dependencyConfig: getDependencyConfig({platformName: 'macos'}), npmPackageName: 'react-native-macos', // [macOS] }, }, diff --git a/yarn.lock b/yarn.lock index 8c3528a47892ca..7f720525dd45e1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12498,6 +12498,7 @@ __metadata: "@jest/create-cache-key-function": "npm:^29.6.3" "@react-native-community/cli": "npm:13.6.9" "@react-native-community/cli-platform-android": "npm:13.6.9" + "@react-native-community/cli-platform-apple": "npm:13.6.9" "@react-native-community/cli-platform-ios": "npm:13.6.9" "@react-native-mac/virtualized-lists": "npm:0.74.87" "@react-native/assets-registry": "npm:0.74.87"