Skip to content

Commit d864bc0

Browse files
authoredJan 8, 2024
feat: adapt buildRun to properly open macOS apps (#2232)
* feat: adapt `buildRun` to install macOS apps * chore: apply reviewers comments * chore: split opening macOS app logic into separate function * fix: get rid of `any` * chore: use `execa`
1 parent 148f4d7 commit d864bc0

File tree

6 files changed

+82
-10
lines changed

6 files changed

+82
-10
lines changed
 

‎packages/cli-platform-apple/src/commands/runCommand/createRun.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {runOnDevice} from './runOnDevice';
3333
import {runOnSimulator} from './runOnSimulator';
3434
import {BuilderCommand} from '../../types';
3535
import {supportedPlatforms} from '../../config/supportedPlatforms';
36+
import openApp from './openApp';
3637

3738
export interface FlagsT extends BuildFlags {
3839
simulator?: string;
@@ -127,7 +128,24 @@ const createRun =
127128
);
128129

129130
if (platformName === 'macos') {
130-
buildProject(xcodeProject, platformName, undefined, mode, scheme, args);
131+
const buildOutput = await buildProject(
132+
xcodeProject,
133+
platformName,
134+
undefined,
135+
mode,
136+
scheme,
137+
args,
138+
);
139+
140+
openApp({
141+
buildOutput,
142+
xcodeProject,
143+
mode,
144+
scheme,
145+
target: args.target,
146+
binaryPath: args.binaryPath,
147+
});
148+
131149
return;
132150
}
133151

‎packages/cli-platform-apple/src/commands/runCommand/getBuildPath.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import {CLIError} from '@react-native-community/cli-tools';
22
import path from 'path';
33
import {BuildSettings} from './getBuildSettings';
4+
import {ApplePlatform} from '../../types';
45

56
export async function getBuildPath(
67
buildSettings: BuildSettings,
8+
platform: ApplePlatform = 'ios',
79
isCatalyst: boolean = false,
810
) {
911
const targetBuildDir = buildSettings.TARGET_BUILD_DIR;
@@ -24,6 +26,8 @@ export async function getBuildPath(
2426

2527
if (isCatalyst) {
2628
return path.join(targetBuildDir, '-maccatalyst', executableFolderPath);
29+
} else if (platform === 'macos') {
30+
return path.join(targetBuildDir, fullProductName);
2731
} else {
2832
return path.join(targetBuildDir, executableFolderPath);
2933
}

‎packages/cli-platform-apple/src/commands/runCommand/installApp.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import chalk from 'chalk';
55
import {getBuildPath} from './getBuildPath';
66
import {getBuildSettings} from './getBuildSettings';
77
import path from 'path';
8+
import {ApplePlatform} from '../../types';
89

910
function handleLaunchResult(
1011
success: boolean,
@@ -19,13 +20,14 @@ function handleLaunchResult(
1920
}
2021

2122
type Options = {
22-
buildOutput: any;
23+
buildOutput: string;
2324
xcodeProject: IOSProjectInfo;
2425
mode: string;
2526
scheme: string;
2627
target?: string;
2728
udid: string;
2829
binaryPath?: string;
30+
platform?: ApplePlatform;
2931
};
3032

3133
export default async function installApp({
@@ -36,6 +38,7 @@ export default async function installApp({
3638
target,
3739
udid,
3840
binaryPath,
41+
platform,
3942
}: Options) {
4043
let appPath = binaryPath;
4144

@@ -52,11 +55,7 @@ export default async function installApp({
5255
}
5356

5457
if (!appPath) {
55-
appPath = await getBuildPath(buildSettings);
56-
}
57-
58-
if (!buildSettings) {
59-
throw new CLIError('Failed to get build settings for your project');
58+
appPath = await getBuildPath(buildSettings, platform);
6059
}
6160

6261
const targetBuildDir = buildSettings.TARGET_BUILD_DIR;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import {CLIError, logger} from '@react-native-community/cli-tools';
2+
import {IOSProjectInfo} from '@react-native-community/cli-types';
3+
import chalk from 'chalk';
4+
import {getBuildPath} from './getBuildPath';
5+
import {getBuildSettings} from './getBuildSettings';
6+
import execa from 'execa';
7+
8+
type Options = {
9+
buildOutput: string;
10+
xcodeProject: IOSProjectInfo;
11+
mode: string;
12+
scheme: string;
13+
target?: string;
14+
binaryPath?: string;
15+
};
16+
17+
export default async function openApp({
18+
buildOutput,
19+
xcodeProject,
20+
mode,
21+
scheme,
22+
target,
23+
binaryPath,
24+
}: Options) {
25+
let appPath = binaryPath;
26+
27+
const buildSettings = await getBuildSettings(
28+
xcodeProject,
29+
mode,
30+
buildOutput,
31+
scheme,
32+
target,
33+
);
34+
35+
if (!buildSettings) {
36+
throw new CLIError('Failed to get build settings for your project');
37+
}
38+
39+
if (!appPath) {
40+
appPath = await getBuildPath(buildSettings, 'macos');
41+
}
42+
43+
logger.info(`Opening "${chalk.bold(appPath)}"`);
44+
45+
try {
46+
await execa(`open ${appPath}`);
47+
logger.success('Successfully launched the app');
48+
} catch (e) {
49+
logger.error('Failed to launch the app', e as string);
50+
}
51+
}

‎packages/cli-platform-apple/src/commands/runCommand/runOnDevice.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export async function runOnDevice(
5757
throw new CLIError('Failed to get build settings for your project');
5858
}
5959

60-
const appPath = await getBuildPath(buildSettings, true);
60+
const appPath = await getBuildPath(buildSettings, platform, true);
6161
const appProcess = child_process.spawn(`${appPath}/${scheme}`, [], {
6262
detached: true,
6363
stdio: 'ignore',
@@ -86,7 +86,7 @@ export async function runOnDevice(
8686
throw new CLIError('Failed to get build settings for your project');
8787
}
8888

89-
appPath = await getBuildPath(buildSettings);
89+
appPath = await getBuildPath(buildSettings, platform);
9090
} else {
9191
appPath = args.binaryPath;
9292
}

‎packages/cli-platform-apple/src/commands/runCommand/runOnSimulator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export async function runOnSimulator(
5656
}
5757

5858
installApp({
59-
buildOutput,
59+
buildOutput: buildOutput ?? '',
6060
xcodeProject,
6161
mode,
6262
scheme,

0 commit comments

Comments
 (0)
Please sign in to comment.