-
Notifications
You must be signed in to change notification settings - Fork 904
/
index.ts
296 lines (265 loc) · 7.59 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import path from 'path';
import chalk from 'chalk';
import {Config} from '@react-native-community/cli-types';
import {
logger,
CLIError,
getDefaultUserTerminal,
} from '@react-native-community/cli-tools';
import {Device} from '../../types';
import {BuildFlags, buildProject} from './buildProject';
import {getDestinationSimulator} from '../../tools/getDestinationSimulator';
import {selectFromInteractiveMode} from '../../tools/selectFromInteractiveMode';
import {getProjectInfo} from '../../tools/getProjectInfo';
import {checkIfConfigurationExists} from '../../tools/checkIfConfigurationExists';
import {getConfigurationScheme} from '../../tools/getConfigurationScheme';
import listIOSDevices from '../../tools/listIOSDevices';
export interface FlagsT extends BuildFlags {
configuration?: string;
simulator?: string;
device?: string | true;
udid?: string;
scheme?: string;
}
async function buildIOS(_: Array<string>, ctx: Config, args: FlagsT) {
if (!ctx.project.ios) {
throw new CLIError(
'iOS project folder not found. Are you sure this is a React Native project?',
);
}
const {xcodeProject, sourceDir} = ctx.project.ios;
if (!xcodeProject) {
throw new CLIError(
`Could not find Xcode project files in "${sourceDir}" folder`,
);
}
process.chdir(sourceDir);
if (args.configuration) {
logger.warn('--configuration has been deprecated. Use --mode instead.');
logger.warn(
'Parameters were automatically reassigned to --mode on this run.',
);
args.mode = args.configuration;
}
const projectInfo = getProjectInfo();
if (args.mode) {
checkIfConfigurationExists(projectInfo, args.mode);
}
const inferredSchemeName = path.basename(
xcodeProject.name,
path.extname(xcodeProject.name),
);
let scheme = args.scheme || inferredSchemeName;
let mode = args.mode;
if (args.interactive) {
const selection = await selectFromInteractiveMode({scheme, mode});
if (selection.scheme) {
scheme = selection.scheme;
}
if (selection.mode) {
mode = selection.mode;
}
}
const modifiedArgs = {...args, scheme, mode};
args.mode = getConfigurationScheme(
{scheme: args.scheme, mode: args.mode},
sourceDir,
);
logger.info(
`Found Xcode ${
xcodeProject.isWorkspace ? 'workspace' : 'project'
} "${chalk.bold(xcodeProject.name)}"`,
);
const extendedArgs = {
...modifiedArgs,
packager: false,
};
// // No need to load all available devices
if (!args.device && !args.udid) {
if (!args.simulator) {
return buildProject(xcodeProject, undefined, scheme, extendedArgs);
}
/**
* If provided simulator does not exist, try simulators in following order
* - iPhone 14
* - iPhone 13
* - iPhone 12
* - iPhone 11
*/
const fallbackSimulators = [
'iPhone 14',
'iPhone 13',
'iPhone 12',
'iPhone 11',
];
const selectedSimulator = getDestinationSimulator(args, fallbackSimulators);
return buildProject(
xcodeProject,
selectedSimulator.udid,
scheme,
extendedArgs,
);
}
if (args.device && args.udid) {
return logger.error(
'The `device` and `udid` options are mutually exclusive.',
);
}
const devices = await listIOSDevices();
if (args.udid) {
const device = devices.find((d) => d.udid === args.udid);
if (!device) {
return logger.error(
`Could not find a device with udid: "${chalk.bold(
args.udid,
)}". ${printFoundDevices(devices)}`,
);
}
return buildProject(xcodeProject, device.udid, scheme, extendedArgs);
} else {
const physicalDevices = devices.filter((d) => d.type !== 'simulator');
const device = matchingDevice(physicalDevices, args.device);
if (device) {
return buildProject(xcodeProject, device.udid, scheme, extendedArgs);
}
}
}
function matchingDevice(
devices: Array<Device>,
deviceName: string | true | undefined,
) {
if (deviceName === true) {
const firstIOSDevice = devices.find((d) => d.type === 'device')!;
if (firstIOSDevice) {
logger.info(
`Using first available device named "${chalk.bold(
firstIOSDevice.name,
)}" due to lack of name supplied.`,
);
return firstIOSDevice;
} else {
logger.error('No iOS devices connected.');
return undefined;
}
}
const deviceByName = devices.find(
(device) =>
device.name === deviceName || formattedDeviceName(device) === deviceName,
);
if (!deviceByName) {
logger.error(
`Could not find a device named: "${chalk.bold(
String(deviceName),
)}". ${printFoundDevices(devices)}`,
);
}
return deviceByName;
}
function formattedDeviceName(simulator: Device) {
return simulator.version
? `${simulator.name} (${simulator.version})`
: simulator.name;
}
function printFoundDevices(devices: Array<Device>) {
return [
'Available devices:',
...devices.map((device) => ` - ${device.name} (${device.udid})`),
].join('\n');
}
export const iosBuildOptions = [
{
name: '--simulator <string>',
description:
'Explicitly set simulator to use. Optionally include iOS version between ' +
'parenthesis at the end to match an exact version: "iPhone 6 (10.0)"',
},
{
name: '--mode <string>',
description:
'Explicitly set the scheme configuration to use. This option is case sensitive.',
},
{
name: '--configuration <string>',
description: '[Deprecated] Explicitly set the scheme configuration to use',
},
{
name: '--scheme <string>',
description: 'Explicitly set Xcode scheme to use',
},
{
name: '--device [string]',
description:
'Explicitly set device to use by name. The value is not required if you have a single device connected.',
},
{
name: '--destination <string>',
description: 'Explicitly extend distination e.g. "arch=x86_64"',
},
{
name: '--udid <string>',
description: 'Explicitly set device to use by udid',
},
{
name: '--verbose',
description: 'Do not use xcbeautify or xcpretty even if installed',
},
{
name: '--port <number>',
default: process.env.RCT_METRO_PORT || 8081,
parse: Number,
},
{
name: '--terminal <string>',
description:
'Launches the Metro Bundler in a new window using the specified terminal path.',
default: getDefaultUserTerminal(),
},
{
name: '--xcconfig [string]',
description: 'Explicitly set xcconfig to use',
},
{
name: '--buildFolder <string>',
description:
'Location for iOS build artifacts. Corresponds to Xcode\'s "-derivedDataPath".',
},
{
name: '--extra-params <string>',
description: 'Custom params that will be passed to xcodebuild command.',
parse: (val: string) => val.split(' '),
},
];
export default {
name: 'build-ios',
description: 'builds your app on iOS simulator',
func: buildIOS,
examples: [
{
desc: 'Build the app for the IOS simulator',
cmd: 'react-native build-ios',
},
{
desc: 'Build the app for all IOS devices',
cmd: 'react-native build-ios --mode "Release"',
},
{
desc: 'Build the app for a specific IOS device',
cmd: 'react-native build-ios --simulator "IPhone 11"',
},
],
options: [
...iosBuildOptions,
{
name: '--interactive',
description:
'Explicitly select which scheme and configuration to use before running a build',
},
],
};