Skip to content

Commit dc5fc0b

Browse files
Lucas Bentodratwas
authored andcommitted
fix: open Metro in the correct terminal (react-native-community#310)
* Put `isPackagerRunning` function on `cli-tools` to share between iOS & Android * Fix `terminal` argument not working when not being provided * Fallback to the default terminal of the machine when starting the packager on `run-android` * Delete `isPackagerRunning` as it was moved to `tools` * Add `terminal` argument to `run-ios` * Launch Metro from within the `runIOS` command * Remove code & add `—terminal` argument * Try using `REACT_TERMINAL` before the default terminal * Put `isPackagerRunning` function on `cli-tools` to share between iOS & Android * Fix `terminal` argument not working when not being provided * Fallback to the default terminal of the machine when starting the packager on `run-android` * Delete `isPackagerRunning` as it was moved to `tools` * Add `terminal` argument to `run-ios` * Launch Metro from within the `runIOS` command * Remove code & add `—terminal` argument * Try using `REACT_TERMINAL` before the default terminal * Add tool function to get the default user terminal * Fix `terminal` arg type * Remove spread and specify entry twice instead * Improve `args` being passed through functions * Reduce code duplication * Put `device` and `udid` variable up in the scope
1 parent 4a81f23 commit dc5fc0b

File tree

5 files changed

+60
-95
lines changed

5 files changed

+60
-95
lines changed

packages/platform-android/src/commands/runAndroid/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ import type {ConfigT} from 'types';
1515

1616
import adb from './adb';
1717
import runOnAllDevices from './runOnAllDevices';
18-
import isPackagerRunning from './isPackagerRunning';
1918
import tryRunAdbReverse from './tryRunAdbReverse';
2019
import tryLaunchAppOnDevice from './tryLaunchAppOnDevice';
2120
import getAdbPath from './getAdbPath';
22-
import {logger} from '@react-native-community/cli-tools';
21+
import {
22+
isPackagerRunning,
23+
logger,
24+
getDefaultUserTerminal,
25+
} from '@react-native-community/cli-tools';
2326

2427
// Verifies this is an Android project
2528
function checkAndroid(root) {
@@ -226,11 +229,7 @@ function installAndLaunchOnDevice(
226229
);
227230
}
228231

229-
function startServerInNewWindow(
230-
port,
231-
terminal = process.env.REACT_TERMINAL,
232-
reactNativePath,
233-
) {
232+
function startServerInNewWindow(port, terminal, reactNativePath) {
234233
/**
235234
* Set up OS-specific filenames and commands
236235
*/
@@ -359,7 +358,7 @@ export default {
359358
command: '--terminal [string]',
360359
description:
361360
'Launches the Metro Bundler in a new window using the specified terminal path.',
362-
default: '',
361+
default: getDefaultUserTerminal(),
363362
},
364363
],
365364
};

packages/platform-ios/src/commands/runIOS/index.js

Lines changed: 47 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@ import type {ConfigT} from 'types';
1717
import findXcodeProject from './findXcodeProject';
1818
import parseIOSDevicesList from './parseIOSDevicesList';
1919
import findMatchingSimulator from './findMatchingSimulator';
20-
import {logger, CLIError} from '@react-native-community/cli-tools';
20+
import {
21+
logger,
22+
CLIError,
23+
getDefaultUserTerminal,
24+
} from '@react-native-community/cli-tools';
2125

2226
type FlagsT = {
2327
simulator: string,
2428
configuration: string,
2529
scheme: ?string,
2630
projectPath: string,
27-
device: ?string,
31+
device: ?(string | true),
2832
udid: ?string,
2933
packager: boolean,
3034
verbose: boolean,
3135
port: number,
36+
terminal: ?string,
3237
};
3338

3439
function runIOS(_: Array<string>, ctx: ConfigT, args: FlagsT) {
@@ -66,65 +71,36 @@ function runIOS(_: Array<string>, ctx: ConfigT, args: FlagsT) {
6671
}),
6772
);
6873

69-
if (args.device) {
70-
const selectedDevice = matchingDevice(devices, args.device);
74+
const device = ((args.device: any): string);
75+
const udid = ((args.udid: any): string);
76+
if (device || udid) {
77+
const selectedDevice = device
78+
? matchingDevice(devices, device)
79+
: matchingDeviceByUdid(devices, udid);
80+
7181
if (selectedDevice) {
72-
return runOnDevice(
73-
selectedDevice,
74-
scheme,
75-
xcodeProject,
76-
args.configuration,
77-
args.packager,
78-
args.verbose,
79-
args.port,
80-
);
82+
return runOnDevice(selectedDevice, scheme, xcodeProject, args);
8183
}
84+
8285
if (devices && devices.length > 0) {
83-
// $FlowIssue: args.device is defined in this context
84-
logger.error(`Could not find device with the name: "${args.device}".
85-
Choose one of the following:${printFoundDevices(devices)}`);
86-
} else {
87-
logger.error('No iOS devices connected.');
86+
const message = device
87+
? `Could not find device with the name: "${device}". Choose one of the following:\n${printFoundDevices(
88+
devices,
89+
)}`
90+
: `Could not find device with the udid: "${udid}". Choose one of the following:\n${printFoundDevices(
91+
devices,
92+
)}`;
93+
94+
return logger.error(message);
8895
}
89-
} else if (args.udid) {
90-
// $FlowIssue: args.udid is defined in this context
91-
return runOnDeviceByUdid(args, scheme, xcodeProject, devices);
92-
}
9396

94-
return runOnSimulator(xcodeProject, args, scheme);
95-
}
96-
97-
function runOnDeviceByUdid(
98-
args: FlagsT & {udid: string},
99-
scheme,
100-
xcodeProject,
101-
devices,
102-
) {
103-
const selectedDevice = matchingDeviceByUdid(devices, args.udid);
104-
105-
if (selectedDevice) {
106-
runOnDevice(
107-
selectedDevice,
108-
scheme,
109-
xcodeProject,
110-
args.configuration,
111-
args.packager,
112-
args.verbose,
113-
args.port,
114-
);
115-
return;
97+
return logger.error('No iOS devices connected.');
11698
}
11799

118-
if (devices && devices.length > 0) {
119-
// $FlowIssue: args.udid is defined in this context
120-
logger.error(`Could not find device with the udid: "${args.udid}".
121-
Choose one of the following:\n${printFoundDevices(devices)}`);
122-
} else {
123-
logger.error('No iOS devices connected.');
124-
}
100+
return runOnSimulator(xcodeProject, scheme, args);
125101
}
126102

127-
async function runOnSimulator(xcodeProject, args, scheme) {
103+
async function runOnSimulator(xcodeProject, scheme, args: FlagsT) {
128104
let simulators;
129105
try {
130106
simulators = JSON.parse(
@@ -175,10 +151,7 @@ async function runOnSimulator(xcodeProject, args, scheme) {
175151
xcodeProject,
176152
selectedSimulator.udid,
177153
scheme,
178-
args.configuration,
179-
args.packager,
180-
args.verbose,
181-
args.port,
154+
args,
182155
);
183156

184157
const appPath = getBuildPath(args.configuration, appName, false, scheme);
@@ -213,34 +186,23 @@ async function runOnSimulator(xcodeProject, args, scheme) {
213186
);
214187
}
215188

216-
async function runOnDevice(
217-
selectedDevice,
218-
scheme,
219-
xcodeProject,
220-
configuration,
221-
launchPackager,
222-
verbose,
223-
port,
224-
) {
189+
async function runOnDevice(selectedDevice, scheme, xcodeProject, args: FlagsT) {
225190
const appName = await buildProject(
226191
xcodeProject,
227192
selectedDevice.udid,
228193
scheme,
229-
configuration,
230-
launchPackager,
231-
verbose,
232-
port,
194+
args,
233195
);
234196

235197
const iosDeployInstallArgs = [
236198
'--bundle',
237-
getBuildPath(configuration, appName, true, scheme),
199+
getBuildPath(args.configuration, appName, true, scheme),
238200
'--id',
239201
selectedDevice.udid,
240202
'--justlaunch',
241203
];
242204

243-
logger.info(`installing and launching your app on ${selectedDevice.name}...`);
205+
logger.info(`Installing and launching your app on ${selectedDevice.name}...`);
244206

245207
const iosDeployOutput = child_process.spawnSync(
246208
'ios-deploy',
@@ -257,21 +219,13 @@ async function runOnDevice(
257219
}
258220
}
259221

260-
function buildProject(
261-
xcodeProject,
262-
udid,
263-
scheme,
264-
configuration,
265-
launchPackager = false,
266-
verbose,
267-
port,
268-
) {
222+
function buildProject(xcodeProject, udid, scheme, args: FlagsT) {
269223
return new Promise((resolve, reject) => {
270224
const xcodebuildArgs = [
271225
xcodeProject.isWorkspace ? '-workspace' : '-project',
272226
xcodeProject.name,
273227
'-configuration',
274-
configuration,
228+
args.configuration,
275229
'-scheme',
276230
scheme,
277231
'-destination',
@@ -281,7 +235,7 @@ function buildProject(
281235
];
282236
logger.info(`Building using "xcodebuild ${xcodebuildArgs.join(' ')}"`);
283237
let xcpretty;
284-
if (!verbose) {
238+
if (!args.verbose) {
285239
xcpretty =
286240
xcprettyAvailable() &&
287241
child_process.spawn('xcpretty', [], {
@@ -291,7 +245,7 @@ function buildProject(
291245
const buildProcess = child_process.spawn(
292246
'xcodebuild',
293247
xcodebuildArgs,
294-
getProcessOptions(launchPackager, port),
248+
getProcessOptions(args),
295249
);
296250
let buildOutput = '';
297251
let errorOutput = '';
@@ -418,15 +372,15 @@ function printFoundDevices(devices) {
418372
return output;
419373
}
420374

421-
function getProcessOptions(launchPackager, port) {
422-
if (launchPackager) {
375+
function getProcessOptions({packager, terminal, port}) {
376+
if (packager) {
423377
return {
424-
env: {...process.env, RCT_METRO_PORT: port},
378+
env: {...process.env, RCT_TERMINAL: terminal, RCT_METRO_PORT: port},
425379
};
426380
}
427381

428382
return {
429-
env: {...process.env, RCT_NO_LAUNCH_PACKAGER: true},
383+
env: {...process.env, RCT_TERMINAL: terminal, RCT_NO_LAUNCH_PACKAGER: true},
430384
};
431385
}
432386

@@ -499,5 +453,11 @@ export default {
499453
default: process.env.RCT_METRO_PORT || 8081,
500454
parse: (val: string) => Number(val),
501455
},
456+
{
457+
command: '--terminal [string]',
458+
description:
459+
'Launches the Metro Bundler in a new window using the specified terminal path.',
460+
default: getDefaultUserTerminal(),
461+
},
502462
],
503463
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const getDefaultUserTerminal = (): ?string =>
2+
process.env.REACT_TERMINAL || process.env.TERM_PROGRAM;
3+
4+
export default getDefaultUserTerminal;

packages/tools/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
*/
44
export {default as logger} from './logger';
55
export {default as groupFilesByType} from './groupFilesByType';
6+
export {default as isPackagerRunning} from './isPackagerRunning';
7+
export {default as getDefaultUserTerminal} from './getDefaultUserTerminal';
68

79
export * from './errors';

0 commit comments

Comments
 (0)