-
Notifications
You must be signed in to change notification settings - Fork 907
/
Copy pathcreateRun.ts
381 lines (337 loc) · 9.78 KB
/
createRun.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* 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 fs from 'fs';
import chalk from 'chalk';
import {Config, IOSProjectConfig} from '@react-native-community/cli-types';
import {
logger,
CLIError,
link,
startServerInNewWindow,
findDevServerPort,
cacheManager,
} from '@react-native-community/cli-tools';
import getArchitecture from '../../tools/getArchitecture';
import listDevices from '../../tools/listDevices';
import {promptForDeviceSelection} from '../../tools/prompts';
import {BuildFlags} from '../buildCommand/buildOptions';
import {buildProject} from '../buildCommand/buildProject';
import {getConfiguration} from '../buildCommand/getConfiguration';
import {getXcodeProjectAndDir} from '../buildCommand/getXcodeProjectAndDir';
import {getFallbackSimulator} from './getFallbackSimulator';
import {getPlatformInfo} from './getPlatformInfo';
import {printFoundDevices, matchingDevice} from './matchingDevice';
import {runOnDevice} from './runOnDevice';
import {runOnSimulator} from './runOnSimulator';
import {BuilderCommand} from '../../types';
import {
supportedPlatforms,
resolvePods,
} from '@react-native-community/cli-config-apple';
import openApp from './openApp';
export interface FlagsT extends BuildFlags {
simulator?: string;
device?: string | true;
udid?: string;
binaryPath?: string;
listDevices?: boolean;
packager?: boolean;
port: number;
terminal?: string;
}
function getPackageJson(root: string) {
try {
return require(path.join(root, 'package.json'));
} catch {
throw new CLIError(
'No package.json found. Please make sure the file exists in the current folder.',
);
}
}
const createRun =
({platformName}: BuilderCommand) =>
async (_: Array<string>, ctx: Config, args: FlagsT) => {
// React Native docs assume platform is always ios/android
link.setPlatform('ios');
const platformConfig = ctx.project[platformName] as IOSProjectConfig;
const {sdkNames, readableName: platformReadableName} =
getPlatformInfo(platformName);
if (
platformConfig === undefined ||
supportedPlatforms[platformName] === undefined
) {
throw new CLIError(
`Unable to find ${platformReadableName} platform config`,
);
}
let {packager, port} = args;
let installedPods = false;
// check if pods need to be installed
if (platformConfig.automaticPodsInstallation || args.forcePods) {
const isAppRunningNewArchitecture = platformConfig.sourceDir
? await getArchitecture(platformConfig.sourceDir)
: undefined;
await resolvePods(
ctx.root,
platformConfig.sourceDir,
ctx.dependencies,
platformName,
{
forceInstall: args.forcePods,
newArchEnabled: isAppRunningNewArchitecture,
},
);
installedPods = true;
}
if (packager) {
const {port: newPort, startPackager} = await findDevServerPort(
port,
ctx.root,
);
if (startPackager) {
await startServerInNewWindow(
newPort,
ctx.root,
ctx.reactNativePath,
args.terminal,
);
}
}
if (ctx.reactNativeVersion !== 'unknown') {
link.setVersion(ctx.reactNativeVersion);
}
let {xcodeProject, sourceDir} = getXcodeProjectAndDir(
platformConfig,
platformName,
installedPods,
);
process.chdir(sourceDir);
if (args.binaryPath) {
args.binaryPath = path.isAbsolute(args.binaryPath)
? args.binaryPath
: path.join(ctx.root, args.binaryPath);
if (!fs.existsSync(args.binaryPath)) {
throw new CLIError(
'binary-path was specified, but the file was not found.',
);
}
}
const {mode, scheme} = await getConfiguration(
xcodeProject,
sourceDir,
args,
platformName,
);
if (platformName === 'macos') {
const buildOutput = await buildProject(
xcodeProject,
platformName,
undefined,
mode,
scheme,
args,
);
openApp({
buildOutput,
xcodeProject,
mode,
scheme,
target: args.target,
binaryPath: args.binaryPath,
});
return;
}
let devices = await listDevices(sdkNames);
if (devices.length === 0) {
return logger.error(
`${platformReadableName} devices or simulators not detected. Install simulators via Xcode or connect a physical ${platformReadableName} device`,
);
}
const packageJson = getPackageJson(ctx.root);
const preferredDevice = cacheManager.get(
packageJson.name,
'lastUsedIOSDeviceId',
);
if (preferredDevice) {
const preferredDeviceIndex = devices.findIndex(
({udid}) => udid === preferredDevice,
);
if (preferredDeviceIndex > -1) {
const [device] = devices.splice(preferredDeviceIndex, 1);
devices.unshift(device);
}
}
const fallbackSimulator =
platformName === 'ios' ? getFallbackSimulator(args) : devices[0];
if (args.listDevices || args.interactive) {
if (args.device || args.udid) {
logger.warn(
`Both ${
args.device ? 'device' : 'udid'
} and "list-devices" parameters were passed to "run" command. We will list available devices and let you choose from one.`,
);
}
const selectedDevice = await promptForDeviceSelection(devices);
if (!selectedDevice) {
throw new CLIError(
`Failed to select device, please try to run app without ${
args.listDevices ? 'list-devices' : 'interactive'
} command.`,
);
} else {
if (selectedDevice.udid !== preferredDevice) {
cacheManager.set(
packageJson.name,
'lastUsedIOSDeviceId',
selectedDevice.udid,
);
}
}
if (selectedDevice.type === 'simulator') {
return runOnSimulator(
xcodeProject,
platformName,
mode,
scheme,
args,
selectedDevice,
);
} else {
return runOnDevice(
selectedDevice,
platformName,
mode,
scheme,
xcodeProject,
args,
);
}
}
if (!args.device && !args.udid && !args.simulator) {
const bootedSimulators = devices.filter(
({state, type}) => state === 'Booted' && type === 'simulator',
);
const bootedDevices = devices.filter(({type}) => type === 'device'); // Physical devices here are always booted
const booted = [...bootedSimulators, ...bootedDevices];
if (booted.length === 0) {
logger.info(
'No booted devices or simulators found. Launching first available simulator...',
);
return runOnSimulator(
xcodeProject,
platformName,
mode,
scheme,
args,
fallbackSimulator,
);
}
logger.info(`Found booted ${booted.map(({name}) => name).join(', ')}`);
for (const simulator of bootedSimulators) {
await runOnSimulator(
xcodeProject,
platformName,
mode,
scheme,
args,
simulator || fallbackSimulator,
);
}
for (const device of bootedDevices) {
await runOnDevice(
device,
platformName,
mode,
scheme,
xcodeProject,
args,
);
}
return;
}
if (args.device && args.udid) {
return logger.error(
'The `device` and `udid` options are mutually exclusive.',
);
}
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)}`,
);
}
if (device.type === 'simulator') {
return runOnSimulator(
xcodeProject,
platformName,
mode,
scheme,
args,
fallbackSimulator,
);
} else {
return runOnDevice(
device,
platformName,
mode,
scheme,
xcodeProject,
args,
);
}
} else if (args.device) {
let device = matchingDevice(devices, args.device);
if (!device) {
const deviceByUdid = devices.find((d) => d.udid === args.device);
if (!deviceByUdid) {
return logger.error(
`Could not find a physical device with name or unique device identifier: "${chalk.bold(
args.device,
)}". ${printFoundDevices(devices, 'device')}`,
);
}
device = deviceByUdid;
if (deviceByUdid.type === 'simulator') {
return logger.error(
`The device with udid: "${chalk.bold(
args.device,
)}" is a simulator. If you want to run on a simulator, use the "--simulator" flag instead.`,
);
}
}
if (device && device.type === 'simulator') {
return logger.error(
"`--device` flag is intended for physical devices. If you're trying to run on a simulator, use `--simulator` instead.",
);
}
if (device && device.type === 'device') {
return runOnDevice(
device,
platformName,
mode,
scheme,
xcodeProject,
args,
);
}
} else {
runOnSimulator(
xcodeProject,
platformName,
mode,
scheme,
args,
fallbackSimulator,
);
}
};
export default createRun;