Skip to content

Commit af162b3

Browse files
Michael S. Kazmierfacebook-github-bot
authored andcommitted
adds --port option to react-native run-ios as well as patches port …
Summary: The pull request adds the `--port` option to `run-ios` allowing a developer to build and launch a react-native app using a single command line like this: ``` react-native run-ios --port 8088 ``` It defaults to the current port 8081. This pull request fixes issue #9145 and issue #14113. This patch also extends `run-android` to properly test and launch the packager with the specified port, extending the work done in PR: ##15316 1. Create a new react-native app, or simply clone this branch and then update your version of react-native using `yarn add file:./path/to/this/fork/of/react-native` 2. run `react-native run-ios --port 8088` 3. watch the packager start on the desired port (8088 in this case) and watch your app in your simulator connect to the packager and launch the app. Closes facebook/react-native#16172 Differential Revision: D6612534 Pulled By: shergin fbshipit-source-id: 50af449f5e4c32fb76ba95f4cb7bf179e35526d5
1 parent 710a7f8 commit af162b3

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

runAndroid/runAndroid.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ function runAndroid(argv, config, args) {
4545
return buildAndRun(args);
4646
}
4747

48-
return isPackagerRunning().then(result => {
48+
return isPackagerRunning(args.port).then(result => {
4949
if (result === 'running') {
5050
console.log(chalk.bold('JS server already running.'));
5151
} else if (result === 'unrecognized') {
5252
console.warn(chalk.yellow('JS server not recognized, continuing with build...'));
5353
} else {
5454
// result == 'not_running'
5555
console.log(chalk.bold('Starting JS server...'));
56-
startServerInNewWindow();
56+
startServerInNewWindow(args.port);
5757
}
5858
return buildAndRun(args);
5959
});
@@ -262,7 +262,7 @@ function runOnAllDevices(args, cmd, packageNameWithSuffix, packageName, adbPath)
262262
}
263263
}
264264

265-
function startServerInNewWindow() {
265+
function startServerInNewWindow(port) {
266266
const scriptFile = /^win/.test(process.platform) ?
267267
'launchPackager.bat' :
268268
'launchPackager.command';
@@ -271,6 +271,12 @@ function startServerInNewWindow() {
271271
const procConfig = {cwd: scriptsDir};
272272
const terminal = process.env.REACT_TERMINAL;
273273

274+
// setup the .packager.env file to ensure the packager starts on the right port
275+
const packagerEnvFile = path.join(__dirname, '..', '..', 'scripts', '.packager.env');
276+
const content = `export RCT_METRO_PORT=${port}`;
277+
// ensure we overwrite file by passing the 'w' flag
278+
fs.writeFileSync(packagerEnvFile, content, {encoding: 'utf8', flag: 'w'});
279+
274280
if (process.platform === 'darwin') {
275281
if (terminal) {
276282
return child_process.spawnSync('open', ['-a', terminal, launchPackagerScript], procConfig);
@@ -333,7 +339,7 @@ module.exports = {
333339
description: 'Do not launch packager while building',
334340
}, {
335341
command: '--port [number]',
336-
default: 8081,
342+
default: process.env.RCT_METRO_PORT || 8081,
337343
parse: (val: string) => Number(val),
338344
}],
339345
};

runIOS/runIOS.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function runIOS(argv, config, args) {
7878
function runOnDeviceByUdid(args, scheme, xcodeProject, devices) {
7979
const selectedDevice = matchingDeviceByUdid(devices, args.udid);
8080
if (selectedDevice) {
81-
return runOnDevice(selectedDevice, scheme, xcodeProject, args.configuration, args.packager, args.verbose);
81+
return runOnDevice(selectedDevice, scheme, xcodeProject, args.configuration, args.packager, args.verbose, args.port);
8282
} else {
8383
if (devices && devices.length > 0) {
8484
console.log('Could not find device with the udid: "' + args.udid + '".');
@@ -115,7 +115,7 @@ function runOnSimulator(xcodeProject, args, scheme) {
115115
}
116116
resolve(selectedSimulator.udid);
117117
})
118-
.then((udid) => buildProject(xcodeProject, udid, scheme, args.configuration, args.packager, args.verbose))
118+
.then((udid) => buildProject(xcodeProject, udid, scheme, args.configuration, args.packager, args.verbose, args.port))
119119
.then((appName) => {
120120
if (!appName) {
121121
appName = scheme;
@@ -135,8 +135,8 @@ function runOnSimulator(xcodeProject, args, scheme) {
135135
});
136136
}
137137

138-
function runOnDevice(selectedDevice, scheme, xcodeProject, configuration, launchPackager, verbose) {
139-
return buildProject(xcodeProject, selectedDevice.udid, scheme, configuration, launchPackager, verbose)
138+
function runOnDevice(selectedDevice, scheme, xcodeProject, configuration, launchPackager, verbose, port) {
139+
return buildProject(xcodeProject, selectedDevice.udid, scheme, configuration, launchPackager, verbose, port)
140140
.then((appName) => {
141141
if (!appName) {
142142
appName = scheme;
@@ -159,7 +159,7 @@ function runOnDevice(selectedDevice, scheme, xcodeProject, configuration, launch
159159
});
160160
}
161161

162-
function buildProject(xcodeProject, udid, scheme, configuration = 'Debug', launchPackager = false, verbose) {
162+
function buildProject(xcodeProject, udid, scheme, configuration = 'Debug', launchPackager = false, verbose, port) {
163163
return new Promise((resolve,reject) =>
164164
{
165165
var xcodebuildArgs = [
@@ -174,7 +174,7 @@ function buildProject(xcodeProject, udid, scheme, configuration = 'Debug', launc
174174
if (!verbose) {
175175
xcpretty = xcprettyAvailable() && child_process.spawn('xcpretty', [], { stdio: ['pipe', process.stdout, process.stderr] });
176176
}
177-
const buildProcess = child_process.spawn('xcodebuild', xcodebuildArgs, getProcessOptions(launchPackager));
177+
const buildProcess = child_process.spawn('xcodebuild', xcodebuildArgs, getProcessOptions(launchPackager, port));
178178
let buildOutput = '';
179179
buildProcess.stdout.on('data', function(data) {
180180
buildOutput += data.toString();
@@ -232,13 +232,15 @@ function printFoundDevices(devices) {
232232
}
233233
}
234234

235-
function getProcessOptions(launchPackager) {
235+
function getProcessOptions(launchPackager, port) {
236236
if (launchPackager) {
237-
return {};
237+
return {
238+
env: { ...process.env, RCT_METRO_PORT: port }
239+
};
238240
}
239241

240242
return {
241-
env: Object.assign({}, process.env, { RCT_NO_LAUNCH_PACKAGER: true }),
243+
env: { ...process.env, RCT_NO_LAUNCH_PACKAGER: true },
242244
};
243245
}
244246

@@ -287,5 +289,9 @@ module.exports = {
287289
}, {
288290
command: '--verbose',
289291
description: 'Do not use xcpretty even if installed',
292+
},{
293+
command: '--port [number]',
294+
default: process.env.RCT_METRO_PORT || 8081,
295+
parse: (val: string) => Number(val),
290296
}],
291297
};

server/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = {
6060
description: 'starts the webserver',
6161
options: [{
6262
command: '--port [number]',
63-
default: 8081,
63+
default: process.env.RCT_METRO_PORT || 8081,
6464
parse: (val: string) => Number(val),
6565
}, {
6666
command: '--host [string]',

util/isPackagerRunning.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ const fetch = require('node-fetch');
1919
* - `unrecognized`: one other process is running on the port we expect the
2020
* packager to be running.
2121
*/
22-
function isPackagerRunning() {
23-
return fetch('http://localhost:8081/status').then(
22+
function isPackagerRunning(packagerPort = (process.env.RCT_METRO_PORT || 8081)) {
23+
return fetch(`http://localhost:${packagerPort}/status`).then(
2424
res => res.text().then(body =>
2525
body === 'packager-status:running' ? 'running' : 'unrecognized'
2626
),

0 commit comments

Comments
 (0)