From 6958ec5d61f3635c333c823793932cc47a16d725 Mon Sep 17 00:00:00 2001 From: Dmytro Klymenko Date: Mon, 6 Sep 2021 08:51:45 +0300 Subject: [PATCH 1/2] #4819 rewrite the check port script with built-in isPackagerRunning function and rename the script --- config/checkMetroBundlerPort.js | 11 ++++++++++ config/checkPort.js | 39 --------------------------------- package.json | 10 ++++----- 3 files changed, 16 insertions(+), 44 deletions(-) create mode 100644 config/checkMetroBundlerPort.js delete mode 100644 config/checkPort.js diff --git a/config/checkMetroBundlerPort.js b/config/checkMetroBundlerPort.js new file mode 100644 index 000000000000..a7ad0bc7621d --- /dev/null +++ b/config/checkMetroBundlerPort.js @@ -0,0 +1,11 @@ +const {isPackagerRunning} = require('@react-native-community/cli-tools'); + +isPackagerRunning().then((result) => { + if (result === 'unrecognized') { + console.error( + 'The port 8081 is currently in use.', + 'You can run `lsof -i :8081` to see which program is using it.\n', + ); + process.exit(1); + } +}); diff --git a/config/checkPort.js b/config/checkPort.js deleted file mode 100644 index 24e3efef3679..000000000000 --- a/config/checkPort.js +++ /dev/null @@ -1,39 +0,0 @@ -const net = require('net'); - -function checkPort(port, host) { - return new Promise((resolve, reject) => { - const server = net.createServer(); - server.once('error', (err) => { - if (err.code !== 'EADDRINUSE') { - return reject(err); - } - - reject(err); - }); - server.once('listening', () => { - server.once('close', () => resolve(false)); - server.close(); - }); - server.listen(port, host); - }); -} - -const inputPort = Number(process.argv.slice(2)[0]); -if (Number.isNaN(inputPort) || inputPort < 0 || inputPort > 65535) { - console.error('This command requires an argument that must be a number [0-65535]\n'); - process.exit(1); -} - -Promise.all([ - checkPort(inputPort, 'localhost'), - checkPort(inputPort, '127.0.0.1'), - checkPort(inputPort, '0.0.0.0'), -]) - .then(() => process.exit(0)) - .catch(() => { - console.error( - `The port ${inputPort} is currently in use.`, - `You can run \`lsof -i :${inputPort}\` to see which program is using it.\n`, - ); - process.exit(1); - }); diff --git a/package.json b/package.json index 80c1cce06c33..4b053901eee4 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "license": "MIT", "private": true, "scripts": { - "android": "npm run check-port -- 8081 && react-native run-android", - "ios": "npm run check-port -- 8081 && react-native run-ios", - "ipad": "npm run check-port -- 8081 && react-native run-ios --simulator=\"iPad Pro (12.9-inch) (4th generation)\"", - "ipad-sm": "npm run check-port -- 8081 && react-native run-ios --simulator=\"iPad Pro (9.7-inch)\"", + "android": "npm run check-metro-bundler-port && react-native run-android", + "ios": "npm run check-metro-bundler-port && react-native run-ios", + "ipad": "npm run check-metro-bundler-port && react-native run-ios --simulator=\"iPad Pro (12.9-inch) (4th generation)\"", + "ipad-sm": "npm run check-metro-bundler-port && react-native run-ios --simulator=\"iPad Pro (9.7-inch)\"", "desktop": "node desktop/start.js", "start": "react-native start", "web": "node web/proxy.js & webpack-dev-server --open --config config/webpack/webpack.dev.js", @@ -30,7 +30,7 @@ "gh-actions-build": "./.github/scripts/buildActions.sh", "gh-actions-validate": "./.github/scripts/validateActionsAndWorkflows.sh", "analyze-packages": "ANALYZE_BUNDLE=true webpack --config config/webpack/webpack.prod.js", - "check-port": "node config/checkPort.js" + "check-metro-bundler-port": "node config/checkMetroBundlerPort.js" }, "dependencies": { "@formatjs/intl-getcanonicallocales": "^1.5.8", From da90f41f56bb44d8d73b1478da3c5659eaf90acc Mon Sep 17 00:00:00 2001 From: Dmytro Klymenko Date: Thu, 9 Sep 2021 19:22:31 +0300 Subject: [PATCH 2/2] #4819 add comment with isPackagerRunning function description --- config/checkMetroBundlerPort.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/config/checkMetroBundlerPort.js b/config/checkMetroBundlerPort.js index a7ad0bc7621d..4c8d017fc1e0 100644 --- a/config/checkMetroBundlerPort.js +++ b/config/checkMetroBundlerPort.js @@ -1,5 +1,12 @@ const {isPackagerRunning} = require('@react-native-community/cli-tools'); +/** + * Function isPackagerRunning indicates whether or not the packager is running. It returns a promise that + * returns one of these possible values: + * - `running`: the packager is running + * - `not_running`: the packager nor any process is running on the expected port. + * - `unrecognized`: one other process is running on the port we expect the packager to be running. + */ isPackagerRunning().then((result) => { if (result === 'unrecognized') { console.error(