Skip to content

Commit

Permalink
fix(react-native): implement projectConfig and dependencyConfig (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Oct 23, 2024
1 parent 1b4dc1b commit 5b7837e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 67 deletions.
37 changes: 0 additions & 37 deletions packages/react-native/local-cli/runMacOS/findXcodeProject.js

This file was deleted.

65 changes: 38 additions & 27 deletions packages/react-native/local-cli/runMacOS/runMacOS.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-check
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
* @format
* @ts-check
*/
'use strict';

Expand All @@ -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');
Expand All @@ -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.',
Expand All @@ -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';
Expand All @@ -77,36 +85,37 @@ function parseArgs(args) {
} "${chalk.bold(xcodeProject.name)}"`,
);

return {xcodeProject, scheme};
return {sourceDir, xcodeProject, scheme};
}

/**
* @param {string[]} _
* @param {Record<string, unknown>} _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<string, unknown>} _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(
Expand Down Expand Up @@ -143,15 +152,17 @@ async function run(xcodeProject, scheme, args) {
}

/**
* @param {string} sourceDir
* @param {XcodeProject} xcodeProject
* @param {string} scheme
* @param {Options} args
* @returns {Promise<void>}
*/
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',
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -186,4 +187,4 @@
}
]
}
}
}
8 changes: 6 additions & 2 deletions packages/react-native/react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -95,8 +99,8 @@ module.exports = {
) => {},
};
},
projectConfig: () => null,
dependencyConfig: () => null,
projectConfig: getProjectConfig({platformName: 'macos'}),
dependencyConfig: getDependencyConfig({platformName: 'macos'}),
npmPackageName: 'react-native-macos', // [macOS]
},
},
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5b7837e

Please sign in to comment.