1
+ // @ts -check
1
2
/**
2
3
* Copyright (c) Microsoft Corporation. All rights reserved.
3
4
* Licensed under the MIT License.
4
5
* @format
5
- * @ts -check
6
6
*/
7
7
'use strict' ;
8
8
20
20
*
21
21
* @typedef {{
22
22
* name: string;
23
+ * path: string;
23
24
* isWorkspace: boolean;
24
25
* }} XcodeProject
26
+ *
27
+ * @typedef {{
28
+ * project?: {
29
+ * macos?: {
30
+ * sourceDir: string;
31
+ * xcodeProject: XcodeProject;
32
+ * }
33
+ * }
34
+ * }} ProjectConfig
25
35
*/
26
36
27
- const findXcodeProject = require ( './findXcodeProject' ) ;
28
37
const chalk = require ( 'chalk' ) ;
29
38
const child_process = require ( 'child_process' ) ;
30
- const fs = require ( 'fs' ) ;
31
39
const path = require ( 'path' ) ;
32
40
const { logger, CLIError, getDefaultUserTerminal} = ( ( ) => {
33
41
const cli = require . resolve ( '@react-native-community/cli/package.json' ) ;
@@ -37,16 +45,11 @@ const {logger, CLIError, getDefaultUserTerminal} = (() => {
37
45
} ) ( ) ;
38
46
39
47
/**
48
+ * @param {ProjectConfig } ctx
40
49
* @param {Options } args
41
- * @returns {{ xcodeProject: XcodeProject, scheme: string } }
50
+ * @returns {{ sourceDir: string, xcodeProject: XcodeProject, scheme: string } }
42
51
*/
43
- function parseArgs ( args ) {
44
- if ( ! fs . existsSync ( args . projectPath ) ) {
45
- throw new CLIError (
46
- 'macOS project folder not found. Are you sure this is a React Native project?' ,
47
- ) ;
48
- }
49
-
52
+ function parseArgs ( ctx , args ) {
50
53
if ( args . configuration ) {
51
54
logger . warn (
52
55
'Argument --configuration has been deprecated and will be removed in a future release, please use --mode instead.' ,
@@ -57,15 +60,20 @@ function parseArgs(args) {
57
60
}
58
61
}
59
62
60
- process . chdir ( args . projectPath ) ;
63
+ const { sourceDir, xcodeProject} = ctx . project ?. macos ?? { } ;
64
+ if ( ! sourceDir ) {
65
+ throw new CLIError (
66
+ 'macOS project folder not found. Are you sure this is a React Native project?' ,
67
+ ) ;
68
+ }
61
69
62
- const xcodeProject = findXcodeProject ( fs . readdirSync ( '.' ) ) ;
63
70
if ( ! xcodeProject ) {
64
71
throw new CLIError (
65
- `Could not find Xcode project files in " ${ args . projectPath } " folder` ,
72
+ 'Xcode project for macOS not found. Did you forget to run `pod install`?' ,
66
73
) ;
67
74
}
68
75
76
+ // TODO: Find schemes using https://github.com/microsoft/rnx-kit/blob/2b4c569cda9e3755ba7afce42d846601bcc7c4e3/packages/tools-apple/src/scheme.ts#L5
69
77
const inferredSchemeName =
70
78
path . basename ( xcodeProject . name , path . extname ( xcodeProject . name ) ) +
71
79
'-macOS' ;
@@ -77,36 +85,37 @@ function parseArgs(args) {
77
85
} "${ chalk . bold ( xcodeProject . name ) } "`,
78
86
) ;
79
87
80
- return { xcodeProject, scheme} ;
88
+ return { sourceDir , xcodeProject, scheme} ;
81
89
}
82
90
83
91
/**
84
92
* @param {string[] } _
85
- * @param {Record<string, unknown> } _ctx
93
+ * @param {ProjectConfig } ctx
86
94
* @param {Options } args
87
95
*/
88
- function buildMacOS ( _ , _ctx , args ) {
89
- const { xcodeProject, scheme} = parseArgs ( args ) ;
90
- return buildProject ( xcodeProject , scheme , { ...args , packager : false } ) ;
96
+ function buildMacOS ( _ , ctx , args ) {
97
+ const { sourceDir , xcodeProject, scheme} = parseArgs ( ctx , args ) ;
98
+ return buildProject ( sourceDir , xcodeProject , scheme , { ...args , packager : false } ) ;
91
99
}
92
100
93
101
/**
94
102
* @param {string[] } _
95
- * @param {Record<string, unknown> } _ctx
103
+ * @param {ProjectConfig } ctx
96
104
* @param {Options } args
97
105
*/
98
- function runMacOS ( _ , _ctx , args ) {
99
- const { xcodeProject, scheme} = parseArgs ( args ) ;
100
- return run ( xcodeProject , scheme , args ) ;
106
+ function runMacOS ( _ , ctx , args ) {
107
+ const { sourceDir , xcodeProject, scheme} = parseArgs ( ctx , args ) ;
108
+ return run ( sourceDir , xcodeProject , scheme , args ) ;
101
109
}
102
110
103
111
/**
112
+ * @param {string } sourceDir
104
113
* @param {XcodeProject } xcodeProject
105
114
* @param {string } scheme
106
115
* @param {Options } args
107
116
*/
108
- async function run ( xcodeProject , scheme , args ) {
109
- await buildProject ( xcodeProject , scheme , args ) ;
117
+ async function run ( sourceDir , xcodeProject , scheme , args ) {
118
+ await buildProject ( sourceDir , xcodeProject , scheme , args ) ;
110
119
111
120
const buildSettings = getBuildSettings ( xcodeProject , args . mode , scheme ) ;
112
121
const appPath = path . join (
@@ -143,15 +152,17 @@ async function run(xcodeProject, scheme, args) {
143
152
}
144
153
145
154
/**
155
+ * @param {string } sourceDir
146
156
* @param {XcodeProject } xcodeProject
147
157
* @param {string } scheme
148
158
* @param {Options } args
159
+ * @returns {Promise<void> }
149
160
*/
150
- function buildProject ( xcodeProject , scheme , args ) {
161
+ function buildProject ( sourceDir , xcodeProject , scheme , args ) {
151
162
return new Promise ( ( resolve , reject ) => {
152
163
const xcodebuildArgs = [
153
164
xcodeProject . isWorkspace ? '-workspace' : '-project' ,
154
- xcodeProject . name ,
165
+ path . join ( sourceDir , xcodeProject . path , xcodeProject . name ) ,
155
166
'-configuration' ,
156
167
args . mode ,
157
168
'-scheme' ,
0 commit comments